大佬教程收集整理的这篇文章主要介绍了iOS:CellForRowAtIndexPath单元格正在混淆,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
iOS: UITableView mixes up data when scrolling too fast
(custom) UITableViewCell’s mixing up after scrolling
Items mixed up after scrolling in UITableView
第一个和最后一个看起来与我的问题非常相关,但是我相当确定每个部分都有逻辑来确定单元格中应该出现什么(数据),但它们仍然混淆了.
以下是相关代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowATindexPath:(NSIndexPath *)indexPath { //Note: the if (cell == nil) thing is no longer required in iOS 6 static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reusEIDentifier:CellIdentifier]; } if (closestRenter != nil) { NSLog(@"CLOSEST RENTER!"); [self setupCellsWithClosestRenterCell:cell aTindexPath:indexPath]; } else { NSLog(@"NO CLOSEST RENTER"); [self setupCellsWithNoClosestRenterCell:cell aTindexPath:indexPath]; } if (indexPath.section == 0) { for (UIView *view in cell.contentView.subviews) { NSLog(@"what THE HECK"); [view removeFromSuperview]; } } return cell;
}
相关信息:
1)ClosestRenter不是……它存在.所以else子句永远不应该执行……就是这样.
2)在代码中:
[self setupCellsWithClosestRenterCell:cell aTindexPath:indexPath];
有一个简单的:
if (indexPath.section == 0) { cell.textLabel.text = @"PLACE HOLDER"; } else { // Populate the cell with data. (creates a view (with controller etC) and loads it into the cell) }
3)任何时候都有2个部分.
问题是第0部分(第一部分)应该只有那个占位符字符串.第1部分应该包含我的自定义子视图(在单元格中,它会这样做).
第0节最初只有占位符字符串,但是一旦我向下滚动(并且该部分不再可见)并向上滚动(快速),它有时会在第1部分中有一个看似随机的单元格…那到底是什么?怎么样?我不愿意把细胞重用归咎于细胞再利用,但在这一点上我真的很傻,我不知道它是什么.
这里令人不安的部分是第0节中的单元格(那里只有一行)没有子视图.但是当我快速向上和向下滚动时,它会得到一个(显然是第1部分),然后我得到了“what THE HECK”日志消息……
值得一提的是,使用for循环(具有heck消息的那个)确实解决了问题(因为它删除了不需要的子视图)但必须有更好的方法.现在感觉不对劲.
有任何想法吗?
谢谢.
我对细胞重用(特别是标识符)的假设就是问题所在.
以前我这样做:
static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reusEIDentifier:CellIdentifier]; }
这是伟大的,但有一个关键问题.所有细胞在技术上都是相同的…在它们全部被分配(而不是零)后,系统无法确定哪个细胞可以重复使用,无论它是什么部分.
这意味着可以从队列中抓取任何单元格,无论它有什么,并且卡在任何地方(尽管我的检查确保第1部分的第1部分,第0部分(假租借者)留在那里) .
解决方案:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowATindexPath:(NSIndexPath *)indexPath { //Note: the if (cell == nil) thing is no longer required in iOS 6 static NSString *CellIdentifier1 = @"Cell"; static NSString *CellIdentifier2 = @"Cell2"; UITableViewCell *cell; if (indexPath.section == 0) { if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reusEIDentifier:CellIdentifier1]; } else { cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; } } else { if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reusEIDentifier:CellIdentifier1]; } else { cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; } } if (closestRenter != nil) { NSLog(@"CLOSEST RENTER!"); [self setupCellsWithClosestRenterCell:cell aTindexPath:indexPath]; } else { NSLog(@"NO CLOSEST RENTER"); [self setupCellsWithNoClosestRenterCell:cell aTindexPath:indexPath]; } return cell;
}
如您所见,第0部分将获得自己的单元标识符.第1部分也是如此.结果是当一个单元格要出列时,它将检查indexPath当前所在的部分并获取正确的单元格.
呃,这么令人沮丧的问题,但现在一切都有道理:)
以上是大佬教程为你收集整理的iOS:CellForRowAtIndexPath单元格正在混淆全部内容,希望文章能够帮你解决iOS:CellForRowAtIndexPath单元格正在混淆所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。