【问题标题】:UITableViewCell duplicated over and overUITableViewCell 一遍又一遍地重复
【发布时间】: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


    【解决方案1】:

    我在下面调整了你的方法。基本上,您需要在创建单元格时使单元格出列并执行任何单元格范围的设置(披露等)。任何单个单元格的更改都应在单元格出列后进行。

    - (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;
    }
    

    【讨论】:

      【解决方案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
      }
      

      【讨论】:

      • 我试过了,虽然它不再重复,但最后一个单元格现在有时会占用其上方其他单元格的文本和字体粗细。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-16
      • 1970-01-01
      • 2021-05-30
      • 1970-01-01
      • 2021-07-05
      • 2017-11-19
      相关资源
      最近更新 更多