我发现一些帖子与我的问题相似但不完全相同。
在我的应用程序中,用户可以在多个 uitableviews 之间导航以深入了解所需的结果。当用户向前、然后向后、然后向前等时,可以注意到行正在被重绘/重写,并且文本变得越来越粗。
我发现在一些帖子中这可能与我创建行的方式有关,在cellforrowatindexpath
method.
我是否需要做一些事情,以便每次用户在表视图之间前进和后退时不会重新填充/重绘行?我是否需要在下面的代码中添加一些内容,或者在 viewwillappear 方法中添加一些内容(当前在表的 viewwillappear 中有一个“reloaddata”,但似乎没有帮助)?
这是我的代码:
- (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];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UILabel *label = [[[UILabel alloc] init] autorelease];
label.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
label.frame = CGRectMake(10.0f, 10.0f, 220.0f, 22.0f);
label.textColor = [UIColor blackColor];
label.backgroundColor = [UIColor clearColor];
label.opaque = NO;
label.text = [mapareaArray objectAtIndex:indexPath.row];
[cell.contentView addSubview:label];
CustomCellBackgroundView *bgView = [[CustomCellBackgroundView alloc] initWithFrame:CGRectZero];
bgView.borderColor = [UIColor clearColor];
bgView.fillColor = [UIColor whiteColor];
bgView.position = CustomCellBackgroundViewPositionSingle;
cell.backgroundView = bgView;
return cell;
}
您遇到的问题是由于这一行:
[cell.contentView addSubview:label];
您正在向表格单元格添加子视图,无论它是否是新单元格。如果它是旧单元格(从可重用池中出列),那么您将向该单元格添加另一个子视图。
相反,您应该标记 UILabel,然后使用标记找到它以修改该 UILabel 的内容。在 if( cell == nil ) 块内添加(并设置其所有属性)并标记 UILabel:
if(cell == nil) {
// alloc and init the cell view...
UILabel *label = [[[UILabel alloc] init] autorelease];
label.tag = kMyTag; // define kMyTag in your header file using #define
// and other label customizations
[cell.contentView addSubview:label]; // addSubview here and only here
...
}
然后使用以下命令找到它:
UILabel *label = (UILabel *)[cell.contentView viewWithTag: kMyTag];
label.text = [mapareaArray objectAtIndex:indexPath.row];
并且无需将其重新添加为 if(cell == nil) 块之外的子视图。子视图已经存在(这就是为什么重用单元格视图效率更高,如果你做得正确的话,那就是;)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)