在uitableviewcell上的tableview中重复行 [英] duplicate rows in tableview on uitableviewcell

查看:119
本文介绍了在uitableviewcell上的tableview中重复行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have found some posts which are similar to my issue but not quite the same.

在我的应用中,用户可以在几个uitableviews之间导航以深入查看期望的结果。当用户前进,然后向后,然后前进等时,可以注意到正在重新绘制/重写行,并且文本变得更大胆。

In my app the user can navigate between several uitableviews to drill down to the desired result. When a user goes forward, then backward, then forward, etc it is noticeable that the rows are being redrawn/re-written and the text gets bolder and bolder.

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

I have found that in some of the posts this may relate to the way that I am creating the rows, using a uilable within the cellforrowatindexpath method.

我需要做些什么,以便每次用户前进和后退时都不会重新填充/重绘行。 tableviews?我是否需要在下面的代码中添加一些东西或者在viewwillappear方法中添加一些东西(目前viewwillappear中有一个'reloaddata'用于表但似乎没有帮助)?

Is there something that I need to do so that the rows are not repopulate/redrawn each time a user goes forward and backward between the tableviews? Do I need to add something to the code below or add something to the viewwillappear method (currently there is a 'reloaddata' in the viewwillappear for the table but doesn't seem to help)?

这是我的代码:

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


推荐答案

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

The problem you are having is due to this line:

[cell.contentView addSubview:label];

您正在向表格单元格添加子视图,无论它是否为新单元格。如果它是一个旧单元格(从可重用池中出列),那么您将向该单元格添加另一个子视图。

You are adding a subview to the table cell whether it's a new cell or not. If it's an old cell (dequeued from the reusable pool), then you will add yet another subview to the cell.

相反,您应该标记UILabel,然后找到它用标签来修改那个UILabel的内容。添加(并设置其所有属性)并在if(cell == nil)块内标记UILabel:

Instead, you should tag the UILabel, and then locate it with the tag to modify the content of that UILabel. Add (and set all of its attributes) and tag the UILabel inside the if( cell == nil ) block:

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

And no need to re-add it as a subview outside of the if(cell == nil) block. The subview is already there (and that's why reusing the cell views are so much more efficient, if you do it correctly, that is ;).

这篇关于在uitableviewcell上的tableview中重复行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆