gpt4 book ai didi

ios - 找不到成员 'CellForItemAtIndexPath'

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

让我描述一下我的问题。

我写了一个名为 CardCell 的类,它继承自 UICollectionViewCell。 在我的另一个类 MainViewController 中,它是 UICollectionView 的子类。创建了一个 NSMutableArray 实例 items 来存储每个单元格的数据。

var items = NSMutableArray()

self.items = NSMutableArray(容量: 20) 因为我在 0...20 { items.addObject("项目 (i)")

items 移动到 CollectionView 的 indexPath 并准备好被调用。

func collectionView(collectionView: UICollectionView!, moveItemAtIndexPath fromIndexPath: NSIndexPath!, toIndexPath: NSIndexPath!) {
var thing: NSString = items[fromIndexPath.item] as NSString
self.items.removeObjectAtIndex(fromIndexPath.item)
self.items.insertObject(thing, atIndex: toIndexPath.item)
}

最后,我尝试使用 items 创建 CardCell 的实例。

var cell: CardCell = CardCell(self.collectionView?.cellForItemAtIndexPath(self.collectionView?.indexPathsForSelectedItems().first))

接下来,出现错误“找不到成员‘CellForItemAtIndexPath’”。 我需要说明的是,这些代码是从 Objective-C 版本翻译过来的。

Objective-C 版本:

 PassCell *cell = (PassCell *)[self.collectionView cellForItemAtIndexPath:[[self.collectionView indexPathsForSelectedItems] firstObject]];

请帮我解决这个问题。

非常感谢您的指导和时间。

伊桑·乔

最佳答案

也许 Objective-C 的翻译出了问题。我认为您不能使用 UICollectionView 实例方法来初始化您的自定义单元格。

在 Objective-C 代码中,您将结果转换为自定义单元格类型。我相信在 Swift 中你应该做同样的事情(注意我将语句分成两部分以提高可读性):

let indexPath = self.collectionView?.indexPathsForSelectedItems().first as IndexPath
var cell = self.collectionView?.cellForItemAtIndexPath(indexPath) as CardCell

注意:根据您的定义,您可能需要 as?as! 作为转换运算符。

关于ios - 找不到成员 'CellForItemAtIndexPath',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29490895/

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