Ios 为什么我的uitableview单元格行重复字幕

Ios 为什么我的uitableview单元格行重复字幕,ios,objective-c,uitableview,Ios,Objective C,Uitableview,在我的uitableview中,我有两个部分 首先是从核心数据中提取 其他通过存储在NSMutableArray(otherFriends)中的textfield添加 这是我的密码 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { if([otherFriends count]>0) { return 2; } return 1; } - (NSInteg

在我的uitableview中,我有两个部分

首先是从核心数据中提取

其他通过存储在NSMutableArray(otherFriends)中的textfield添加

这是我的密码

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if([otherFriends count]>0)
    {
        return 2;
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
    if(otherFriends == 0)
    {
        return [[[[self fetchedResultsController]sections]objectAtIndex:0]numberOfObjects];
    }
    return [otherFriends count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"newGroupCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Light" size:20.0];
    cell.textLabel.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.0f];

    if(indexPath.section == 0)
    {
        user = [[self fetchedResultsController] objectAtIndexPath:indexPath];

        cell.textLabel.text = user.name;
        cell.detailTextLabel.text = user.primaryResource.status;

        [cell.imageView setFrame:CGRectMake(0, 0, 50, 50)];
        [cell.imageView.layer setMasksToBounds:YES];

        if (user.photo != nil)
        {
            cell.imageView.image = user.photo;
        }
        else
        {
            cell.imageView.image = [UIImage imageNamed:@"defaultPerson"];
        }
    }
    else
    {
        cell.textLabel.text = [otherFriends objectAtIndex:indexPath.row];
        cell.imageView.image = [UIImage imageNamed:@"defaultPerson"];
    }

    return cell;
}
第一部分也有字幕,但第二部分单元格没有字幕

当我添加新朋友时,它可以正常工作,直到所有行都可见,但当添加新朋友时,该行不可见,要查看该行,我必须滚动,然后该行显示第0节(第一个单元格)中第一行的副标题,并在第1节第三行重复该副标题。只有副标题是重复的,但正文不是


几个小时以来,我一直在试图弄明白这一点,但运气不好

这是因为在
else
分支中,您没有设置
单元格.detailtextlab.text

当一个单元格被回收时,旧的detailTextLabel会留在那里。您需要在conditionsl的两个分支中设置单元格的所有属性,前提是它已被回收

if(indexPath.section == 0)
{
    user = [[self fetchedResultsController] objectAtIndexPath:indexPath];

    cell.textLabel.text = user.name;
    cell.detailTextLabel.text = user.primaryResource.status;

    [cell.imageView setFrame:CGRectMake(0, 0, 50, 50)];
    [cell.imageView.layer setMasksToBounds:YES];

    if (user.photo != nil)
    {
        cell.imageView.image = user.photo;
    }
    else
    {
        cell.imageView.image = [UIImage imageNamed:@"defaultPerson"];
    }
}
else
{
    cell.textLabel.text = [otherFriends objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:@"defaultPerson"];
    // ADDED
    cell.detailTextLabel.text = @"";
    // You may also need to adjust the frame of the cell.imageView
    // because it could have been recycled.
}

细胞被重新利用。由于两个部分使用相同的单元格,因此必须确保在所有条件下设置/重置相同的单元格属性集

问题是当单元格用于第1节时,您没有重置
detailtextlab
。在
else
块中,添加:

cell.detailTextLabel.text = nil;

这确保在所有情况下,您都在为重复使用的单元格设置
textlab
detailtextlab
,以及
imageView
属性。

感谢您的回复。。。我有另一个问题,现在我必须提供一个选择取消选择选项类似于电子邮件选择和用户也可以选择取消选择随机行。这也造成了复选标记显示不正确的问题。@S.J您必须对单元格的附件视图执行几乎相同的操作:无论通过
cellForRowIndexPath:
方法的代码采用何种路径,单元格的所有元素都必须写入。换句话说,如果您只写一次,则无论您使用
if
的哪个分支,每次都必须对其进行写入。编辑显示的问题与原始问题无关。考虑回滚编辑和写入一个单独的关于行的程序选择的问题。我可以马上告诉你,你这样做是完全错误的。不过,我不愿意写一个答案,因为它与你最初的问题完全无关。创建一个新问题,称之为“如何以编程方式选择UITableView行”,并粘贴第二个代码段。@dasbLinkedLight请查看我的新问题