ios UITableView 单元格附件视图图像问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15349983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:40:09  来源:igfitidea点击:

UITableView cell accessoryView image issue

iosxcodeuitableview

提问by mindbomb

I am trying to display a 'padlock' icon on specific rows of my UITableViewCells using this code:

我正在尝试使用以下代码在 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;
}

I am getting a funny result - the padlock is initially shown on rows 5,9 but when I scroll down and up the list the icon gets redisplayed by random on other cells' accessoryView as well (there's only 1 section btw), and the scrolling becomes quite jerky and laggy... The more I scroll up/down the more instances of it are displayed! Why? where's the bug here?

我得到了一个有趣的结果 - 挂锁最初显示在第 5,9 行,但是当我向下和向上滚动列表时,图标也会随机重新显示在其他单元格的附件视图上(顺便说一句,只有 1 个部分),并且滚动变得非常生涩和迟钝......我向上/向下滚动得越多,显示的实例就越多!为什么?这里的错误在哪里?

help, thanks!

帮助,谢谢!

回答by calimarkus

The cells get reused. You need to reset the accessoryView each time. Its just a small change:

细胞得到重用。您每次都需要重置附件视图。它只是一个小小的变化:

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

Just for completion, you can also use Eric's solution like this - it may be faster, since the imageView is not created every time. But thats only a minimal difference. Probably your lags have other reasons.

只是为了完成,您也可以像这样使用 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;
}

回答by Eric Qian

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    if(indexPath.row == 5 || indexPath.row == 9) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCellWithImage"];
    } else {
        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;
}

回答by Dary

To showing custom accessoryView cell in Swift 4 & Swift 5

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