繁体   English   中英

UITableView的怪异行为

[英]Weird behavior from UITableView

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

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

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

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

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

我怀疑这种行为是由于表视图重用单元格引起的,但我仍然无法弄清楚。

代码段:

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

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

您有几种选择:

  • 在黑色区域使用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;
}

对于每种不同类型的单元,您需要一个不同的重用标识符。 因此,您的案例需要三个reuseIdentifier。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM