我从一个数组中创建我的单元格。我区域中的最后一个对象的创建方式不同,因为我想将新的UILabel添加到单元格的contentView中。 (以便它显示最后一个单元格中UILabel的记录总数)。UITableViewCell一遍又一遍地重复
出于某种原因,我最后一个将UILabel添加到contentView的单元格不断得到重复。它会出现在其他单元格上,搞砸了他们的显示器等。我有一种感觉,这与细胞再利用有关,但我不知道如何解决它。
这里是我的方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCellID = @"cellID";
// Bounds
CGRect bounds = [[UIScreen mainScreen] bounds];
Person *person = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
person = [people objectAtIndex:indexPath.row];
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellID] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Check if it is the first cell
if (person == [people lastObject]) {
cell.accessoryType = UITableViewCellAccessoryNone;
cell.userInteractionEnabled = NO;
UILabel *count = [[UILabel alloc] initWithFrame:CGRectMake(-4, 11, bounds.size.width - 10, 20)];
count.autoresizingMask = UIViewAutoresizingFlexibleWidth;
count.textColor = [UIColor darkGrayColor];
count.font = [UIFont systemFontOfSize:16];
count.textAlignment = UITextAlignmentCenter;
count.text = person.nameFirst;
cell.textLabel.text = nil;
cell.detailTextLabel.text = nil;
[cell.contentView addSubview:count];
return cell;
}
}//end
cell.textLabel.text = [NSString stringWithFormat:@"%@, %@", person.nameLast, person.nameFirst];
// Check if they are faculty staff
if ([person.status isEqualToString:@"Staff/Faculty"]) {
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@: %@", person.status, person.department];
} else {
cell.detailTextLabel.text = person.status;
}//end
return cell;
}
有人可以帮助我明白,我怎么能得到这个工作正常,使我的UILabel没有得到在不同的细胞产生的?
这工作完美!感谢您清理这个过程。 – 2011-02-15 21:03:20