我有以下问题:
我有一个带有一些文本和一些图像作为内容的 UITableView。单元格的 TextLabel 显示文本,我将带有一些 UIImageViews 的 UIView 作为其子视图添加到单元格内容视图。
一切正常,直到我删除一些单元格。发生的情况是,我从表中删除了一个单元格(比如说第一个),重新加载它的数据,然后第二个单元格再向上移动一个,但是!右侧的 UIView 包含来自第一个(已删除)单元格的内容。
我不知道为什么会发生这种情况——尽管我认为 cellForRowAtIndexPath 回调方法有问题。
我将粘贴一些代码以使其更清晰。
我能做的是卸载控制器并再次加载它 - 然后图像再次包含正确的内容......
这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifierUNCHECK = @"MyIdentifierUNCHECK";
MyStuff *m = [allObjects objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:MyIdentifierUNCHECK];
UIView *v;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifierUNCHECK] autorelease];
v = [[[UIView alloc] initWithFrame:CGRectMake(5.0, 5.0, 70.0, 70.0)] autorelease];
v.contentMode = UIViewContentModeScaleAspectFill;
[v addSubview:[[UIImageView alloc] init]];//some image view
[cell.contentView addSubview:v];
}
cell.textLabel.text = m.name;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica-BoldOblique" size:14.0];
cell.imageView.image = [UIImage imageNamed:@"checkbox.png"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;