gpt4 book ai didi

ios - 为什么 dequeueReusableCell(withReuseIdentifier :for:) is nil outside the cellForItemAtIndexPath function scope?

转载 作者:行者123 更新时间:2023-11-28 15:01:18 27 4
gpt4 key购买 nike

您好,我试图在 NSFetchedResultsController 的委托(delegate)函数中修改 UICollectionView 的单元格

controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?)

通过 dequeueReusableCell(withReuseIdentifier:for:) 函数。但它总是返回零。所以我不得不使用 cellForItemAtIndexPath 函数。但是为什么 dequeueReusableCell 返回 nil。当然,我之前已经注册过类(class)。

 collectionView?.register(MessageCell.self, forCellWithReuseIdentifier: cellId)

代码如下:

   if type == .update {
print("UPDATE")
blockOperations.append(BlockOperation(block: {
let cell = self.collectionView?.cellForItem(at: indexPath!)
as! MessageCell
// let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: self.cellId, for: indexPath!) returns nil. why?
let friend = self.fetchedResultsController.object(at: indexPath!) as! Friend
cell.message = friend.lastMessage

}))
}

最佳答案

首先你确实需要 cellForItem ,其次它会返回一个当前可见的单元格

   collec.cellForItem(at: NSIndexPath.init(row: 0, section: 0  ) as IndexPath)

关于ios - 为什么 dequeueReusableCell(withReuseIdentifier :for:) is nil outside the cellForItemAtIndexPath function scope?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48884730/

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