UITableViewCell分配问题 – 单元格不是零

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell2"; UILabel *titleLabel; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 300, 20)]; [cell.contentView addSubview:titleLabel]; titleLabel.tag = 0011; } else { titleLabel = (UILabel *)[cell.contentView viewWithTag:0011]; } // Configure the cell... NSMutableString *title = [NSMutableString stringWithString:@"Customer: "]; [title appendString:[titles objectAtIndex:indexPath.row]]; titleLabel.text = title.copy; titleLabel.backgroundColor = [UIColor clearColor]; titleLabel.textAlignment = UITextAlignmentLeft; titleLabel.font = [UIFont systemFontOfSize:18.0]; 

我的单元从来没有零,我的标题标签,因为从来没有分配,虽然我的细胞生成。 我看不出这是怎么可能的。 if状态从来不是真的,这应该是第一次生成的细胞,但是我的细胞是按照它们应该的方式创build的,没有标题标签

这听起来好像你正在使用iOS 5(或更高版本)和故事板。

在iOS 5(或更高版本)下,如果您正在使用Storyboard和TableView控制器,则dequeueReusableCellWithIdentifier:方法将保证返回一个单元格(前提是您已经在Storyboard中定义了具有给定标识符的单元格)。

如果是这种情况,解决scheme是在Storyboard中完全创build自定义表格单元格。 转到“故事板”中的“表格视图”,select“ Content:Dynamic Prototypes然后制作Prototype Cells:1 。 现在,以graphics的方式来布局你的单元格,正是你想要的。 现在点击单元格并设置Identifier:Cell2 。 您现在不需要在运行时创build标签或检查它是否为零。 包括如何引用您设置的标签的详细信息,请参阅iOS 5发行说明(下面的链接)或networking上的许多教程。

请参阅iOS 5发行说明部分configuration表视图