具有动态像元高度的Swift水平collectionview

时间:2019-10-11 13:56:43

标签: swift uicollectionview autolayout size

我在垂直滚动视图中有两个水平collectionview。顶部collectionview具有固定的单元格高度300,但是底部collectionview应该具有基于内容大小的动态高度。我这样做了:

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    if collectionView == headerCollectionView {
        return CGSize(width: view.bounds.width, height: 300)
    }
    if collectionView == contentCollectionView {
        return CGSize(width: view.bounds.width, height: contentHeight.constant)
    }
    return CGSize(width: 0, height: 0)
}

然后在我的cellForItemAt方法中,我试图更改contentHeight约束,但是这样做之后,即使将contentTopConstraint设置为0,我的内容也会向下移动。

我在这里可以做什么?

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if collectionView == contentCollectionView {
        let contentCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContentCell", for: indexPath) as! ContentCell
        contentCell.setNames(currentBlock.items[indexPath.row])
        if contentCell.name.countLabelLines() > 1 {
           self.contentHeight.constant += 34
        }[![enter image description here][1]][1]
        APIManager.load(item_id: currentBlock.items[indexPath.row].id) { (apiItem, error) in
            if error == nil {
                DispatchQueue.main.async {
                   contentCell.setContent(apiItem!)
                }
            }
        }
        return contentCell
    }
    return UICollectionViewCell()
}

enter image description here

0 个答案:

没有答案
相关问题