目前我正在做一个项目和开始的开发人员 用于访问单元格的项目
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/