6

我发现了一些与我的问题相似但不完全相同的帖子。

在我的应用程序中,用户可以在几个 uitableviews 之间导航以深入了解所需的结果。当用户向前,然后向后,然后向前等时,很明显,行正在重新绘制/重写,并且文本变得越来越粗。

我发现在某些帖子中,这可能与我在方法中使用 uilable 创建行的cellforrowatindexpath方式有关。

有什么我需要做的,以便每次用户在表格视图之间前进和后退时不会重新填充/重绘行吗?我是否需要在下面的代码中添加一些内容或向 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;
}
4

3 回答 3

11

您遇到的问题是由于这一行:

[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) 块之外的子视图。子视图已经存在(这就是为什么重用单元格视图效率更高的原因,如果你做得正确,那就是;)。

于 2009-08-25T09:15:17.007 回答
1

.h 文件 'define' 放在头文件顶部的#import 语句之后,并且被放置为 0,因为我不知道如何定义它:

#define kMyTag 0

.m 文件我已根据您的评论更新了此部分,但是 a) 未填充表,b) 当用户导航到下一个视图并返回此视图时,它失败并显示“发送到实例的无法识别的选择器” , 和 c) 我必须放入两个“返回单元”;条目或它跌倒。我想我的东西顺序错误,也许没有正确初始化东西????

- (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;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        UILabel *label = [[[UILabel alloc] init] autorelease];
        label.tag = kMyTag; // define kMyTag in your header file using #define
        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;

        CustomCellBackgroundView *bgView = [[CustomCellBackgroundView alloc] initWithFrame:CGRectZero];
        bgView.borderColor = [UIColor clearColor];
        bgView.fillColor = [UIColor whiteColor];
        bgView.position = CustomCellBackgroundViewPositionSingle;
        cell.backgroundView = bgView;

        [cell.contentView addSubview:label]; // addSubview here and only here
        return cell;
    }
UILabel *label = (UILabel *)[cell.contentView viewWithTag: kMyTag];
label.text = [mapareaArray objectAtIndex:indexPath.row];
return cell;
}
于 2009-08-25T12:27:26.397 回答
0

您应该为该特定单元格创建 UITableViewCell 的子类,然后应用逻辑以查看是否每次都需要将子视图添加到自身。此外,使用 UITableviewcell 的 prepareForReuse 并在此期间删除任何子视图,然后再应用您是否要向 Cell 添加 UILabel 的逻辑。

于 2014-06-17T15:57:29.953 回答