【问题标题】:Why cell's childs' height changes during scrolling?为什么在滚动过程中单元格的孩子的高度会发生变化?
【发布时间】:2018-02-26 11:33:59
【问题描述】:

使用UIController(以新信息重用单元格 - 与另一个模型类实例)和自定义UICollectionViewCell 在滚动期间(用手指从上到下)单元格标签的高度从 54.0 到 84.5(见输出)

为什么会这样?

测试代码

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyItemCollectionViewCell", for: indexPath) as! MyItemCollectionViewCell

    print("uilabel height \(cell.mylabel.frame.height)")

    return cell
}

输出

uilabel height 54.0
uilabel height 54.0
uilabel height 54.0
uilabel height 54.0
uilabel height 54.0
uilabel height 54.0
uilabel height 54.0
uilabel height 84.5
uilabel height 84.5
uilabel height 84.5
uilabel height 84.5

单元格

class MyItemCollectionViewCell: UICollectionViewCell {

    @IBOutlet var mylabel: UILabel!

    func setItem(_ item: MyItem) {
        mylabel.text = item.name
    }
}

【问题讨论】:

  • 每个单元格的 itemSize 可能不同
  • @Sh_Khan 我没有改变大小,我在故事板中只有一个单元格及其子级的模板
  • 你的意思是它的变化是明显的还是只是在“打印”中?另外,您在哪里调用“setItem”,它在“cellForItemAtIndexPath”中吗?我在您的代码中没有看到任何内容。
  • 另外一点,“cellForItemAtIndexPath”可能无法显示正确的高度,所以不要依赖“打印”来调试单元格高度。
  • @aunnnn 见cellForItemAt indexPath

标签: ios swift uicollectionview uicollectionviewcell


【解决方案1】:

我认为可能是重绘屏幕时的问题,集合视图没有重新加载。看起来和这篇文章一样CollectionView Cell changing size after scrolling - AutoLayout

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-23
相关资源
最近更新 更多