UITableView中水平UICollectionView的表视图单元格大小



我在一个UITableView内设置了多个水平滚动的UICollectionView,因此我可以垂直滚动。

我遇到的一个问题是,在我知道它们应该有多高之前,就调用了表视图的行高度(我希望每个UICollectionView的整个高度都能填充表视图单元格)。

我该如何解决这个问题?

我的建议是为集合视图创建一个自定义布局。然后,它可以计算单元格大小,并在调整集合视图的大小时得到通知。

使用此函数可以确定collectionView的高度。

aCollectionView.collectionViewLayout属性上调用collectionViewContentSize,并以CGSize的形式获取内容的高度和宽度。

-(CGFloat)collectionViewHeight
{
  [collectionView layoutIfNeeded];
  return [collectionView contentSize].height;
}

为您的身高创建一个变量,并在heightForRowAtIndexPath方法中返回该变量。计算出身高后,重新加载tableView。

var height: CGFloat = 44
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return height
}
func something() {
    height = 1234//Or whatever your height should be
    self.table.reloadData()
}

最新更新