使用dequeueReusableCellWithIdentifier工作的前7个单元,然后它随机开始重用从1-7的细胞8-14等等.然而,它应该创造新的细胞,因为它们还不存在。这是我的密码-
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
NSLog(indexPath.description)
var cell = (tableView.dequeueReusableCellWithIdentifier("matchCell", forIndexPath: indexPath)) as! MatchCell
if (cell.created == nil) {
cell = configureCell(cell, withIndex: indexPath.section)
}
return cell
}不知道为什么会这样。前7个细胞很好..。
发布于 2016-02-16 23:07:28
单元格是可重用的,因此一旦它们离开屏幕,它们将从dequeueReusableCellWithIdentifier返回给您,因此对于重用的单元格来说,您的if (cell.created == nil)不会是正确的。
解决这一问题的方法有两种:
cell.created == nilprepareForReuse中重写MatchCell,并为重用进行任何清理(并将created设置为零)我个人喜欢第二种选择。苹果文档有关于prepareForReuse的更多信息。
https://stackoverflow.com/questions/35444618
复制相似问题