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

标签 ios objective-c uitableview

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

首先从核心数据中获取

other 是通过存储在 NSMutableArray(otherFriends) 中的文本字段添加的

这是我的代码

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

第一部分也有字幕,但第二部分单元格没有字幕

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

我花了几个小时试图弄清楚这一点,但没有运气。

最佳答案

这是因为在您的 else 分支中,您没有设置 cell.detailTextLabel.text

当单元格被回收时,旧的detailTextLabel会保留在那里。您需要在条件的两个分支中设置单元格的所有属性,以防单元格已被回收。

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

关于ios - 为什么我的 uitableview 单元格行重复字幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14402488/

相关文章:

iphone - Documents 目录中图像的图像缓存

ios - 应用程序崩溃时移至第一个屏幕/主屏幕

ios - 此类与键 [Tableview 错误] 的键值编码不兼容

iphone - 在 iOS 5 中获取访问 token 后获取 Facebook 用户个人资料数据

ios - tableView 数据源在 viewDidLoad() 中为空 - Swift

iphone - 在 iPhone 中显示 RSS Feed`

objective-c - 如何获取 Cocoa/Obj-C 中两个或多个文件的共同祖先目录?

ios - 静态单元格的内容未出现在 UITableView 中

ios - 从 TableView 中删除行

iOS objective-C : How to call Push Segue from another Class