2011-02-15 114 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

相關問題