【发布时间】: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