【问题标题】:Assign tags correctly to cells and subviews of uitableview将标签正确分配给 uitableview 的单元格和子视图
【发布时间】:2012-10-28 00:38:42
【问题描述】:

嘿伙计们,我已经连续 2 天被这个问题困住了,所以我想问问有没有人可以帮我一把。

我有一个由 4 个部分组成的表格视图。

第 1 部分 ->仅由 1 行组成,其中其单元格包含一个子视图,该子视图是一个 uiimageview

第 2 部分 -> 由 2 个普通行组成(只有 2 个简单的单元格,其中包含文本)

第 3 节 -> 由 1 个普通行组成

第 4 节 -> 由 1 行组成,其中的单元格包含一个子视图,该子视图是一个可以包含动态文本的 uitextview,因此 uitextview 的高度以及单元格的高度会根据 uitextview 中有多少文本而变化。

创建此结构的代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


//create a nsstring object that we can use as the reuse identifier
static NSString *CellIdentifier = @"Cell";

//check to see if we can reuse a cell from a row that has just rolled off the screen
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

//if there re no cells that can be reused, create a new cell
if(cell==nil){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];


    switch (indexPath.section) {
        case 0:
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell.contentView addSubview:_viewForImageHeader];

            break;

        case 1:
            cell.selectionStyle = UITableViewCellSelectionStyleGray;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

            cell.textLabel.numberOfLines = 0;
            cell.textLabel.lineBreakMode = 0;
            cell.textLabel.font = [UIFont fontWithName:@"AmericanTypewriter" size:16.0];

            break;

        case 2:
            cell.selectionStyle = UITableViewCellSelectionStyleGray;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.font = [UIFont fontWithName:@"AmericanTypewriter" size:16.0];
            break;

        default:
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell.contentView addSubview:_textViewForArticle];

            break;

    }

}
else{

    NSLog(@"in else");

}


//here i fill in the 2 normal cells with text


return cell;

}

当 uitableview 加载(纵向模式)时,一切都很完美(图像在第 1 节,第 2 节和第 3 节包含正确的文本,在第 4 节我有我的动态文本)。但是当我开始旋转应用程序时,所有的单元格都混在一起了。例如,我在第 4 节中找到第 3 节的内容,反之亦然。

我认为这与我可能没有正确重用单元格的事实有关。我应该使用标签吗?如果是,在特定情况下如何实现标签的使用?

【问题讨论】:

    标签: ios xcode uitableview cell


    【解决方案1】:

    switch case 放在if 条件之外 - 在if and else 之后

    【讨论】:

    • 你是个天才!非常感谢!
    • 我正在尝试接受您的回答,但系统告诉我必须再等 3 分钟。再次感谢!
    【解决方案2】:

    是的,这是因为重复使用单元格。这里有几个选项,但是如果这个 tableView 中的单元格永远不会超过 5 个,那么到目前为止,最简单和最优化的解决方案是不重用单元格。换句话说,不是调用“dequeueReusableCellWithIdentifier”,而是每次分配一个新的单元格。

    如果您有更多单元格,这会降低性能,但如果您的单元格是静态且有限的(就像它们看起来那样),尝试重用单元格并没有真正的好处。

    如果您确实有 4 种“类型”的单元格和更多行(更动态分配的单元格),则解决方案是为 4 种“类型”的单元格中的每一种子类 UITableViewCell,然后根据部分或任何您的标准。

    【讨论】:

    • 非常感谢@HarryStack。因此,按照您告诉我的内容,如果我使用 user427969 的解决方案,我每次都会创建一个新单元格,因此单元格不会混淆。在我的项目中,我使用了 user427969 解决方案,但我非常感谢您的意见,我会记住您告诉我的内容以供将来参考。
    • 我很想赞成这个答案以及 user427969 的答案,但我没有 15 个代表点 :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多