gpt4 book ai didi

swift - 找不到委托(delegate)方法索引路径

转载 作者:行者123 更新时间:2023-11-28 08:11:58 25 4
gpt4 key购买 nike

我已经为 UICollectionViewDelegateFlowLayout 实现了这个委托(delegate)方法,但是当试图从那里取出单元格时,我得到这个错误:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

如果我尝试这样做,我也会得到这个:let cell = collectionView.cellForItem(at: indexPath) as! RQTTipCell

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TipCell", for: indexPath) as! RQTTipCell
let height = self.proxyView!.tipCollect.contentSize.height
let width = cell.bagRatio.multiplier / height
return CGSize(width: width, height: height)

}

这个indexPath应该是正确的,但好像不是。

最佳答案

如果您检查错误含义,则意味着您尝试从数组中获取的项目不可用。

 *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

您正在尝试获取索引 1 处的内容,但您的数组大小为 [0 .. 0]

现在为什么会这样?

  • 简单的解释是,当您尝试从 CollectionView 获取 Cell 时,它在该移动中不可用。原因可能是 Cell 已添加。

现在如何修复,在这里使用optional cast。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TipCell", for: indexPath) as? RQTTipCell {
let height = self.proxyView!.tipCollect.contentSize.height
let width = cell.bagRatio.multiplier / height
return CGSize(width: width, height: height)
} else {
// default CGSzie
CGSize()
}

}

关于swift - 找不到委托(delegate)方法索引路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43502511/

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