如何调整UICollectionView单元格的大小以适应其中的内容?

时间:2017-01-08 20:06:25

标签: ios swift uicollectionview swift3

我想在sizeForItemAt中这样做,但我无法访问那里的单元格内容的高度。尝试在cellForItemAt中执行此操作,因为我们在那里创建了单元格的实例并且可以在那里引用它的所有内容及其高度,但是在此方法中设置单元格的帧大小并不是&# 39;似乎工作。似乎只能在sizeForItemAt中正确设置大小。这是我尝试在cellForItemAt中执行此操作的一个示例(请注意,在此方法中,我添加了likesCommentsLabel视图,具体取决于它是否为其.text分配了字符串,这就是高度动态的原因:

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let feedCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! FeedCell

    feedCell.post = posts[indexPath.item] //This executes post setter and didSet in FeedCell class

    //Determine whether to add likesCommentsLabel
    if (feedCell.likesCommentsLabel.text != nil) {
        feedCell.addSubview(feedCell.likesCommentsLabel)
        feedCell.addConstraintsWithFormat(format: "H:|-12-[v0]|", views: feedCell.likesCommentsLabel)
        feedCell.addConstraintsWithFormat(format: "V:|-8-[v0(44)]-4-[v1]-4-[v2(200)]-8-[v3(24)]-8-[v4(0.4)][v5(44)]|", views: feedCell.profileImageView, feedCell.statusTextView, feedCell.statusImageView, feedCell.likesCommentsLabel, feedCell.dividerLineView, feedCell.credibilityButton)

        print("THIS RUNS")
    }
    else {
        feedCell.addConstraintsWithFormat(format: "V:|-8-[v0(44)]-4-[v1]-4-[v2(200)]-8-[v3(0.4)][v4(44)]|", views: feedCell.profileImageView, feedCell.statusTextView, feedCell.statusImageView, feedCell.dividerLineView, feedCell.credibilityButton)
        print("THIS RUNS MORE")
    }

    //Determine size of cell based on status text  length
    if let statusText = posts[indexPath.item].statusText {
        let rect = NSString(string: statusText).boundingRect(with: CGSize(width: view.frame.width, height: 500), options: NSStringDrawingOptions.usesFontLeading.union(NSStringDrawingOptions.usesLineFragmentOrigin), attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 14)], context: nil)

        var knownHeight: CGFloat = 8.0 + 44.0 + 4.0 + 4.0 + 200.0 + 8.0 + 44.0

        //Add to height of cell, if there's a likes/comments label
        if (feedCell.likesCommentsLabel.text != nil) {
            knownHeight = CGFloat(8.0 + 44.0 + 4.0 + 4.0 + 200.0 + 8.0 + 24.0 + 8.0 + 44.0)
        }
        else {
            knownHeight = CGFloat(8.0 + 44.0 + 4.0 + 4.0 + 200.0 + 8.0 + 44.0)
        }
        //Calculate y position based off of previous cell. If no previous cell, y = 0.
        if indexPath.item > 0 {
            feedCell.frame = CGRect(x: 0, y: ((collectionView.cellForItem(at: collectionView.indexPathsForVisibleItems[indexPath.item - 1])?.frame.origin.y)! +  (collectionView.cellForItem(at: collectionView.indexPathsForVisibleItems[indexPath.item - 1])?.frame.height)!), width: view.frame.width, height: rect.height + knownHeight + 20)
        }
        else {
            feedCell.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: rect.height + knownHeight + 20)
        }
        print(feedCell.frame)
    }

    return feedCell
}

1 个答案:

答案 0 :(得分:0)

覆盖'viewDidLayoutSubviews'方法并使集合视图的布局无效,例如

 - (void)viewDidLayoutSubviews {
     [super viewDidLayoutSubviews];
  [yourCollectionView.collectionViewLayout invalidateLayout];
 }

然后'sizeForItemAtIndexPath'将起作用!