gpt4 book ai didi

ios - collectionView.cellForItem(在 : indexPath) not working properly

转载 作者:行者123 更新时间:2023-11-29 00:27:46 25 4
gpt4 key购买 nike

我读过让我的 collectionViewController 成为 UICollectionViewDelegateFlowLayout 的委托(delegate),我已经正确注册了我的单元格, collectionView?.register(ActivityCell.self, forCellWithReuseIdentifier: cellId) ,我已经正确地将单元格取出并归还了它

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

return cell
}

但每当我尝试使用 sizeForItemAtIndexPath 函数中的 indexPath 检索单元格时,它不会返回事件单元格

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if let cell: ActivityCell = self.collectionView?.cellForItem(at: indexPath) as? ActivityCell
{
let approximatewidthofcell = view.frame.size.width - 50
let size = CGSize(width: approximatewidthofcell, height: 1000)
let attributes = [NSFontAttributeName: UIFont.systemFont(ofSize: 15)]

let estimatedframe = NSString(string: cell.activitylabel.text!).boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attributes, context: nil)
return CGSize(width: view.frame.size.width, height: estimatedframe.height)
}
else
{
return CGSize(width: view.frame.size.width, height: 200)
}

}

If block 总是被忽略,而 else block 每次都执行,我不知道接下来要操作什么代码,请帮助

最佳答案

我有一段时间没有看 Collection View ,但我认为这是一个顺序的事情,这个方法在 cellForItem 之前被调用。另外,你分配数据源了吗?

关于ios - collectionView.cellForItem(在 : indexPath) not working properly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42605896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com