2011-02-15 61 views
1

我从一个数组中创建我的单元格。我区域中的最后一个对象的创建方式不同,因为我想将新的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没有得到在不同的细胞产生的?

回答

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; 
} 
+0

这工作完美!感谢您清理这个过程。 – 2011-02-15 21:03:20

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 
} 
+0

我试过了,虽然它不再被重复,但最后一个单元现在有时会承担上面其他单元的文本和字体重量。 – 2011-02-15 20:58:18

相关问题