美文网首页
iOS tableview和collectionView高度自适

iOS tableview和collectionView高度自适

作者: 漫步云端_91cd | 来源:发表于2023-12-13 15:24 被阅读0次

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;
});

相关文章

网友评论

      本文标题:iOS tableview和collectionView高度自适

      本文链接:https://www.haomeiwen.com/subject/ahnbgdtx.html