在uitableviewcell上的tableview中重复行

时间:2009-08-25 09:06:55

标签: 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;
}

3 个答案:

答案 0 :(得分: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)块之外的子视图。子视图已经存在(这就是为什么重用单元格视图效率更高,如果你正确地执行它,那就是;)。

答案 1 :(得分:1)

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

#define kMyTag 0

.m文件 我已根据您的评论更新了此部分,但a)未填充表格,b)当用户导航到下一个视图并返回此视图时,它失败并显示“无法识别的选择器已发送到实例”,并且我不得不放入两个'返回牢房';条目或它倒下。我认为我的事情顺序错误,可能没有正确地初始化事情????

- (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;
}

答案 2 :(得分:0)

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

相关问题