【发布时间】:2011-02-15 20:39:23
【问题描述】:
我正在从一个数组创建我的单元格。我所在区域的最后一个对象的创建方式不同,因为我想将一个新的 UILabel 添加到单元格的 contentView 中。 (以便它在最后一个单元格中显示 UILabel 中的记录总数)。
由于某种原因,我最后一个将 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 不会在不同的单元格上创建吗?
【问题讨论】:
标签: iphone ios uitableview uilabel