gpt4 book ai didi

ios - dequeueReusableCell 时 Swift 自定义 CollectionViewCell 转换失败

转载 作者:可可西里 更新时间:2023-11-01 03:24:12 25 4
gpt4 key购买 nike

当我尝试将 dequeueReusableCell 转换为 MenuCollectionViewCell 时出现以下错误,我已为其创建文件并将 Storyboard中的单元格更改为该类。我正在关注这个 tutorial来自 raywenderlich。我已经用示例检查了代码,但找不到错误。 导致此错误的常见错误有哪些?

如果我将转换改回 as UICollectionViewCell 那么错误就会消失。

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as MenuCollectionViewCell


cell.backgroundColor = UIColor.lightGrayColor()


return cell
}

错误:

libswiftCore.dylib`swift_dynamicCastClassUnconditional:
0x10bd39860: pushq %rbp
0x10bd39861: movq %rsp, %rbp
0x10bd39864: testq %rdi, %rdi
0x10bd39867: je 0x10bd3989e ; swift_dynamicCastClassUnconditional + 62
0x10bd39869: movabsq $-0x7fffffffffffffff, %rax
0x10bd39873: testq %rax, %rdi
0x10bd39876: jne 0x10bd3989e ; swift_dynamicCastClassUnconditional + 62
0x10bd39878: leaq 0xb52e9(%rip), %rax
0x10bd3987f: movq (%rax), %rax
0x10bd39882: andq (%rdi), %rax
0x10bd39885: nopw %cs:(%rax,%rax)
0x10bd39890: cmpq %rsi, %rax
0x10bd39893: je 0x10bd398ad ; swift_dynamicCastClassUnconditional + 77
0x10bd39895: movq 0x8(%rax), %rax
0x10bd39899: testq %rax, %rax
0x10bd3989c: jne 0x10bd39890 ; swift_dynamicCastClassUnconditional + 48
0x10bd3989e: leaq 0x36b7d(%rip), %rax ; "Swift dynamic cast failed"
0x10bd398a5: movq %rax, 0xb4c0c(%rip) ; gCRAnnotations + 8
0x10bd398ac: int3
0x10bd398ad: movq %rdi, %rax
0x10bd398b0: popq %rbp
0x10bd398b1: retq
0x10bd398b2: nopw %cs:(%rax,%rax)

更新:如果尝试使用下面的代码检查单元格类型并得到 false,即使我已将它设置为 Storyboard 中的 MenuCollectionViewCell

    print( cell is MenuCollectionViewCell ) => false

Storyboard cell setting

最佳答案

问题出在 UICollectionViewController 模板随附的 viewDidLoad() 方法中的以下代码。我删除了它,一切都按预期工作。

self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

关于ios - dequeueReusableCell 时 Swift 自定义 CollectionViewCell 转换失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28327099/

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