0

嘿伙计们,我已经连续两天被这个问题困住了,所以我问是否有任何人可以帮助我。

我有一个由 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 节的内容,反之亦然。

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

4

2 回答 2

0

switch案例置于外部if条件 - 之后if and else

于 2012-11-07T21:31:16.393 回答
0

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

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

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

于 2012-11-07T21:37:15.147 回答