ios - : tableView. cellForRow 和 dataSource.cellForRowAtIndexPath 之间的区别

标签 ios swift uikit tableview

目前我正在做一个项目和开始的开发人员 用于访问单元格的项目

dataSource.cellForRowAtIndexPath(indexPath)

但我习惯于使用

访问单元格
cellForRow(at: indexPath)

有谁知道其中的区别,哪个更好用?我问的原因是 dataSource.cellForRowAtIndexPath(indexPath) 导致了我们这次崩溃。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempted to dequeue multiple cells for the same index path, which is not allowed. If you really need to dequeue more cells than the table view is requesting, use the -dequeueReusableCellWithIdentifier: method (without an index path). Cell identifier: CheckTableViewCell, index path: {length = 2, path = 1 - 0}'

最佳答案

区别在于:

dataSource.cellForRowAtIndexPath(indexPath)

cellForRowAtIndexPath 是您的dataSource 提供给UITableView 的方法。它由 iOS 调用以加载特定 indexPath 的单元格。它的工作原理是将可重复使用的单元格出列并填充该单元格的信息。

您通常不会调用它。 iOS 可以。

cellForRow(at: indexPath)

此方法由 iOS 提供,用于返回特定 indexPath 的单元格(如果该单元格当前在屏幕上),或者返回 nil(如果该单元格不在屏幕上)。

如果您有一个 indexPath 并且您需要关联的单元格,您可以调用此方法。


您的错误发生是因为您正在调用 cellForRowAtIndexPath,它使可重复使用的单元格出列以完成其工作。该单元格已经在屏幕上并且已经有一个与其关联的单元格,所以现在有两个,这没有意义。

关于ios - : tableView. cellForRow 和 dataSource.cellForRowAtIndexPath 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51170020/

相关文章:

ios - 自定义字体未显示在新 Storyboard中

ios - 找出 Music.app 中正在播放的歌曲

ios - 创建需要 NSCoder 的子类实例

ios - 激活元素时如何获取indexpath.row?

ios - 当我更改数据源中的项目数时,UIcollection View 崩溃

cocoa - 将核心数据实体绑定(bind)到 NSTableView : Model Key not working

swift - Swift 中的 Urlencode 西里尔字符

uicollectionview - 如何在 Collection View 中手动选择下一个聚焦索引路径

ios - 如何识别左/右滑动手势?比 UISwipeGestureRecognizer.direction.right/left 更宽松

iphone - UITableViewCell : How to prevent blue selection background w/o borking isSelected property?