Iphone 从UITableView中退出可重用单元队列
此代码运行良好的原因:Iphone 从UITableView中退出可重用单元队列,iphone,objective-c,uitableview,deque,Iphone,Objective C,Uitableview,Deque,此代码运行良好的原因: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifi
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text = [NSString stringWithFormat:@"cell%i%i", indexPath.section, indexPath.row];
}
return cell;
}
就我所理解的单元格标识符而言,只有当我将
cell.textLabel.text=…
行移出if语句时,此代码才能正常工作。换句话说,为什么标签上有正确的文本?尝试创建比屏幕上看到的更多的单元格,一旦它们退出队列,它们将不再有您期望的文本
在屏幕上看到5行左右基本上是可以的,但一旦开始滚动,就会看到一些“有趣”的东西:)所创建的单元格被重用。这意味着对象被标记为可重用(从而保存对象的完整创建) 因此,一旦滚动出屏幕,一个单元格就被标记为可重用。因此,在创建新单元格之前,首先检查是否存在任何可重用单元格([tableView dequeueReusableCellWithIdentifier:)
您需要设置的文本,因为它(可能)与表中的每个单元格不同,但与对象创建/销毁无关。即使您来回滚动表视图,所有单元格的值都是正确的?或者仅适用于当前可见的单元格?