iOS tableview和collectionView高度自适应

1.父tableview 的cell里面嵌套 子tableview,父级cell高度自适应撑起
1)子级cell高度固定,可以通过数组数量*cell的高度计算tableview的高度,撑起父级cell
在父级cell里面进行刷新高度,方法如下:
a.在赋值数组是刷新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高度自适应时
通过一下方法获取子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高度自适应方法:
[self.CollectionView reloadData];
// 延迟一秒去执行 成功回调
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;
});

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容