Iphone uitableviewcell上的tableview中存在重复行

Iphone uitableviewcell上的tableview中存在重复行,iphone,uitableview,uilabel,Iphone,Uitableview,Uilabel,我发现一些帖子与我的问题相似,但不完全相同 在我的应用程序中,用户可以在多个UITableView之间导航,以向下搜索到所需的结果。当用户向前、向后、向前等时,可以注意到行正在被重新绘制/重新写入,文本越来越粗体 我发现在一些帖子中,这可能与我使用cellforrowatinexpath方法中的uilable创建行的方式有关 我是否需要做些什么,以便在每次用户在TableView之间前后移动时不会重新填充/重新绘制行?我是否需要向下面的代码中添加一些内容,或者向ViewWillDisplay方法

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

在我的应用程序中,用户可以在多个UITableView之间导航,以向下搜索到所需的结果。当用户向前、向后、向前等时,可以注意到行正在被重新绘制/重新写入,文本越来越粗体

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

我是否需要做些什么,以便在每次用户在TableView之间前后移动时不会重新填充/重新绘制行?我是否需要向下面的代码中添加一些内容,或者向ViewWillDisplay方法中添加一些内容(目前ViewWillDisplay中有一个“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:

然后使用以下工具定位它:

UILabel *label = (UILabel *)[cell.contentView viewWithTag: kMyTag];
label.text = [mapareaArray objectAtIndex:indexPath.row];
并且无需将其作为if(cell==nil)块外部的子视图重新添加。子视图已经存在(这就是为什么如果正确地执行,重用单元视图会更加高效,也就是;)。

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

您应该为该特定单元格的UITableViewCell创建一个子类,然后应用逻辑查看是否每次都需要将子视图添加到自身中。另外,在应用是否要向单元格添加UILabel的逻辑之前,请使用UITableviewcell的PrepareForuse并删除在此期间的所有子视图。

非常感谢您的帮助。帮了大忙。最后一个问题-我没有编程背景,所以不确定你所说的头文件中的use#define是什么意思。这样做的语法是什么?我以前见过它,并试图将“#define kMyTag”放在标题中,但这不起作用。。。我假设您需要将kMyTag定义为一个值,但不确定执行此操作的语法是什么。你能帮忙吗?我不确定你是否应该用0来表示kMyTag的值。尝试从1开始,因为0可能是所有内容的默认值。更改后,移除if块内的返回单元格。
- (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;
}