iOS tableview和collectionView高度自適應(yīng)

1.父tableview 的cell里面嵌套 子tableview,父級cell高度自適應(yīng)撐起
1)子級cell高度固定,可以通過數(shù)組數(shù)量*cell的高度計算tableview的高度,撐起父級cell
在父級cell里面進行刷新高度,方法如下:
a.在賦值數(shù)組是刷新tableview
[self.tableView reloadData];
[self.contentView layoutIfNeeded];
[self layoutIfNeeded];
b.
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSInteger index = 1;
index = self.model.goods.count;
CGFloat height = (100 + 16) * index;
self.tableViewHeightCons.constant = height;
return index;

}

2)子級cell高度自適應(yīng)時
通過一下方法獲取子tableview的高度

pragma mark reload 完tableview,獲取tableview的contentSize

  • (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath )indexPath {
    if([indexPath row] == ((NSIndexPath
    )[[tableView indexPathsForVisibleRows] lastObject]).row){
    dispatch_async(dispatch_get_main_queue(),^{
    self.tableViewHeightCons.constant = tableView.contentSize.height;
    });
    }
    }

3.collectionView高度自適應(yīng)方法:
[self.CollectionView reloadData];
// 延遲一秒去執(zhí)行 成功回調(diào)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.CollectionViewHeightCons.constant = self.infoCollectionView.collectionViewLayout.collectionViewContentSize.height;
});

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容