【问题标题】:Weird behavior from UITableViewUITableView 的奇怪行为
【发布时间】:2011-09-11 06:51:06
【问题描述】:

我正在尝试使用两种不同类型的 UITableViewCell 打印 UITableView。

  1. 空的表格视图单元格不包含任何内容(前 2 个单元格),只有黑色背景
  2. 表格视图单元格包含从 1 开始的标签、图像等(第 3 个和后续单元格)

我的表格视图的高度可以随时容纳至少 3 个表格视图单元格。

当我第一次加载表格视图时,表格视图看起来非常好 - 第一两行是黑色的,接下来是第三行,其标签上写着“1”。

但是,在向下滚动到底部后又向上滚动。我的前两个空单元格(应该是空的)充满了只能在第三个和后续单元格中找到的东西。

我怀疑这种行为来自重复使用单元格的表格视图,但我仍然无法弄清楚。

代码sn-ps:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }    

    switch ([indexPath row]) {
        case 0:
            cell.contentView.backgroundColor = [UIColor blackColor];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
            break;
        case 1:
            cell.contentView.backgroundColor = [UIColor blackColor];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
            break;

        default:
            cell.contentView.backgroundColor = [UIColor blackColor];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 

            UILabel *rowLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 30, 200, 30)];
            rowLabel.text = [NSString stringWithFormat:@"Row %d", [indexPath row]-1];
            rowLabel.backgroundColor = [UIColor blackColor];
            rowLabel.textColor = [UIColor whiteColor];
            [rowLabel setFont:[UIFont fontWithName:@"GillSans-Bold" size:18]];
            [cell.contentView rowLabel];                 [rowLabel release];

            UIButton *aButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 50, 40, 40)];
            [aButton setImage:anImage forState:UIControlStateNormal];
            [cell.contentView addSubview:aButton];
            [aButton release];

            break;
    }

    return cell;
}

【问题讨论】:

  • 我可能已经找到了我的解决方案。我接受了使用唯一重用标识符的建议。我对每个细胞都这样做了。而且,我将开关盒移到了 if 中。当 stackoverflow 允许时,我会发布我的答案!
  • 这不是正确的解决方案,您应该尽可能重复使用单元格,否则您的滚动可能会很慢。
  • @jrturton,感谢您告诉我!当您说重用时,您的意思是重用单元格内的每一件事(包括按钮、图像)吗?
  • 我已经更新了我的答案,这是你应该做的事情。

标签: iphone ios ipad uitableview


【解决方案1】:

您的出列单元格仍将包含您添加的所有按钮和标签,您所做的只是在单元格出列时设置背景颜色。

你有几个选择:

  • 为黑色区域使用tableHeaderView
  • 使用带有部分的分组表视图
  • 对第 0 行和第 1 行的单元格使用不同的重用标识符。

对于后一种选择,您的cellForRowAtIndexPath 应该是:

static NSString *CellIdentifier = @"Cell";
static NSString *blankCellIndentifier = @"BlankCell";

if (indexPath.row == 0 || indexPath.row == 1)
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:blankCellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:blankCellIdentifier] autorelease];
        cell.contentView.backgroundColor = [UIColor blackColor];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
return cell;
}
else
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UILabel *rowLabel;
    UIButton *aButton;

    if (cell == nil) 
    {
        // Here, you create all of the new objects
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        cell.contentView.backgroundColor = [UIColor blackColor];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
        rowLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 30, 200, 30)];
        rowLabel.backgroundColor = [UIColor blackColor];
        rowLabel.textColor = [UIColor whiteColor];
        [rowLabel setFont:[UIFont fontWithName:@"GillSans-Bold" size:18]];
        [cell.contentView addSubview:rowLabel];
        rowLabel.tag = 1;
        [rowLabel release];

        aButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 50, 40, 40)];
        [aButton setImage:anImage forState:UIControlStateNormal];
        [cell.contentView addSubview:aButton];
        aButton.tag = 2;
        [aButton release];

    }
    // Here, you just configure the objects as appropriate for the row 
    rowLabel = (UILabel*)[cell.contentView viewWithTag:1];
    rowLabel.text = [NSString stringWithFormat:@"Row %d", [indexPath row]-1];

    return cell;
}

【讨论】:

  • 我正在尝试对第 0 行和第 1 行的单元格使用不同的重用标识符。示例:“CellBlank”和“Cell”?第 2 行的 CellBlank 第 3 行和后续行的 Cell?
  • 非常感谢。我刚刚实现了这些代码,到目前为止我的应用程序运行良好!
【解决方案2】:

对于每种不同类型的单元格,您需要不同的重用标识符。所以,你的情况你需要三个reuseIdentifier。

【讨论】:

  • 嗨@Benjamin Mayo,为什么是 3 而不是 2?因为我有两种不同类型的细胞?
  • 您在上面的代码中有三个单元格。当行位于索引 0、索引 1 和任何其他行时。这是三种类型的细胞。
  • 我已经得到了上面的答案。也感谢您的帮助。
猜你喜欢
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多