SKa*_*ser 6 iphone uitableview uilabel
我发现了一些与我的问题类似的帖子,但不完全相同.
在我的应用程序中,用户可以在几个uitableview之间导航以深入查看所需的结果.当用户前进,然后向后,然后前进等时,可以注意到正在重新绘制/重写行,并且文本变得更大胆.
我发现在某些帖子中,这可能与我创建行的方式有关,使用cellforrowatindexpath方法中的uilable .
是否有一些我需要做的事情,以便每次用户在tableviews之间前进和后退时不会重新填充/重绘行?我是否需要在下面的代码中添加一些东西或者在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;
}
Run Code Online (Sandbox Code Playgroud)
tch*_*hen 11
你遇到的问题是由于这一行:
[cell.contentView addSubview:label];
Run Code Online (Sandbox Code Playgroud)
您正在向表格单元格添加子视图,无论它是否是新单元格.如果它是一个旧单元格(从可重用池中出列),那么您将向该单元格添加另一个子视图.
相反,您应该标记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
...
}
Run Code Online (Sandbox Code Playgroud)
然后找到它:
UILabel *label = (UILabel *)[cell.contentView viewWithTag: kMyTag];
label.text = [mapareaArray objectAtIndex:indexPath.row];
Run Code Online (Sandbox Code Playgroud)
并且无需将其重新添加为if(cell == nil)块之外的子视图.子视图已经存在(这就是为什么重用单元格视图效率更高,如果你正确地执行它,那就是;).
| 归档时间: |
|
| 查看次数: |
6073 次 |
| 最近记录: |