UITableViewCell一遍又一遍地重复

时间:2011-02-15 20:39:24

标签: iphone ios uitableview uilabel

我正在从数组中创建我的单元格。我区域中的最后一个对象的创建方式不同,因为我想将新的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不会在不同的单元格上创建吗?

2 个答案:

答案 0 :(得分:2)

这可能是因为细胞会被重复使用。为最后一个单元使用不同的重用标识符,例如kLastCellID。

UITableViewCell *cell = nil;
// Check if it is the first cell
if (person == [people lastObject]) {
    cell = [tableView dequeueReusableCellWithIdentifier:kLastCellID];
    if (!cell) {
        //create a last cell, with an UILabel in your content view
    }
    //update the cell's content
} else {
    cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
    if (!cell) {
        //create a regular cell
    }
    //update the cell's content
}

答案 1 :(得分:2)

我已经调整了你的方法。基本上,您需要在创建单元格时将单元格出列并执行任何单元格范围设置(公开等)。任何单个细胞更换都应在细胞出列后进行。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *kCellID = @"cellID";
    static NSString *lastCellID = @"lastcellID";

    // Bounds
    CGRect bounds = [[UIScreen mainScreen] bounds];

    Person *person = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        person = [people objectAtIndex:indexPath.row];
    }

    UITableViewCell *cell;

    if (person != [people lastObject]) {
        cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellID] autorelease];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        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
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:lastCellID];
        if (cell == nil) {
           cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:lastCellID] autorelease];
           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.tag = 1;
            [cell.contentView addSubview:count];
        }
        UILabel *count = (UILabel *)[cell viewWithTag:1];
        count.text = person.nameFirst;
        //cell.textLabel.text = nil;
        //cell.detailTextLabel.text = nil;
    }//end

    return cell;
}