嘿伙计们,我已经连续两天被这个问题困住了,所以我问是否有任何人可以帮助我。
我有一个由 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 节的内容,反之亦然。
我认为这与我可能没有正确重用单元格的事实有关。我应该使用标签吗?如果是,在特定情况下如何实现标签的使用?