UITableViewCells乱序

时间:2012-01-25 02:10:19

标签: objective-c uitableview

好的,我有另一个UITableView问题。由于某种原因,indexPath.row都混乱了。当我注释掉设置单元格的if语句时,一切正常。 NSLogs告诉我他们按顺序加载,但是所有单元都没有故障。

它们似乎也在重复;我只看到8个细胞,它们一遍又一遍地重复着。

这是我的代码:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";
NSLog(@"row: %d",indexPath.row);

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    cell.contentView.backgroundColor = [UIColor clearColor];

    // Add subviews like this:
    // [[cell contentView] addSubview:objectName];
    // And I get the row number like this: indexPath.row when getting objects from the array

}

return cell;
}

1 个答案:

答案 0 :(得分:0)

现在设置的方式cell.selectionStylecell.backgroundColorcell.contentView.backgrounColor等只有在if (cell == nil)为真时才会设置。您需要将该代码移到if语句块之外,以便在dequeueReusableCellWithIdentifier:生成单元格时以及库存中没有单元格时生成任何内容(即nil)。

相关问题