我從一個數組中創建我的單元格。我區域中的最後一個對象的創建方式不同,因爲我想將新的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