Ios UITableView单元格访问视图图像问题

Ios UITableView单元格访问视图图像问题,ios,xcode,uitableview,Ios,Xcode,Uitableview,我试图使用以下代码在UITableViewCells的特定行上显示“挂锁”图标: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCell"]; GPBTopic *t

我试图使用以下代码在UITableViewCells的特定行上显示“挂锁”图标:

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

    GPBTopic *topic = [self.topics.list objectAtIndex:indexPath.row];
    cell.textLabel.text= topic.name;

    if ((indexPath.row == 5) || (indexPath.row == 9))
    {
            cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"lock_icon.png"]];;
            [cell.accessoryView setFrame:CGRectMake(0, 0, 24, 24)];

    }

    return cell;
}
我得到了一个有趣的结果-挂锁最初显示在第5、9行,但当我向下和向上滚动列表时,图标也会在其他单元格的accessoryView上随机重新显示(顺便说一句,只有1个部分),滚动变得非常急促和滞后。。。我向上/向下滚动的次数越多,显示的实例就越多! 为什么?这里的虫子在哪里


救命,谢谢

细胞被重新利用。每次都需要重置accessoryView。 这只是一个小小的变化:

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

    GPBTopic *topic = [self.topics.list objectAtIndex:indexPath.row];
    cell.textLabel.text= topic.name;

    if ((indexPath.row == 5) || (indexPath.row == 9))
    {
      cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"lock_icon.png"]];
      [cell.accessoryView setFrame:CGRectMake(0, 0, 24, 24)];
    } else {
      cell.accessoryView = nil; 
    }

    return cell;
}
作为补充,您也可以像这样使用Eric的解决方案-它可能更快,因为imageView不是每次都创建的。但这只是一个极小的区别。也许你的滞后还有其他原因

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    if(indexPath.row == 5 || indexPath.row == 9) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCellWithImage"];
        cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"lock_icon.png"]];;
        [cell.accessoryView setFrame:CGRectMake(0, 0, 24, 24)];
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCell"];
    }

    GPBTopic *topic = [self.topics.list objectAtIndex:indexPath.row];
    cell.textLabel.text= topic.name;

    return cell;
}

在Swift 4和Swift 5中显示自定义附件视图单元格

let lockIcon = UIImage(named: "lock_icon")
let lockIconView = UIImageView(image: lockIcon)
lockIconView.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
cell.accessoryView = lockIconView

因为当你滚动时,单元格已经被其他行重复使用,我认为你应该为第5行和第9行使用不同的CellReuseIdentifier好的,谢谢!我稍后再查。但实际上我需要没有图像的单元格来显示“公开”的灰色直角图标5和9仍然会显示图像。其他人没有。只要试一下,并阅读uitableview文档-它解释了reusing.works,但仍然有点滞后。谢谢接受你的回答很酷。谢谢您的第一个答案实际上更简单,效果也很好,事实上,滞后是由于其他原因(模拟器的东西)。:)抱歉,不正确,Jayde3的解决方案要简单得多。。谢谢
let lockIcon = UIImage(named: "lock_icon")
let lockIconView = UIImageView(image: lockIcon)
lockIconView.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
cell.accessoryView = lockIconView