You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS中设置相同UITableViewCell accessoryView出现卡顿问题求助

解决UITableViewCell设置相同accessoryView时的卡顿问题

嘿,这个卡顿问题我之前也碰到过!大概率是你在配置UITableViewCell的时候重复创建了相同的accessoryView实例——毕竟tableView滚动时会快速复用cell,每次都新建一个view对象,会让主线程在滚动时承担过多的创建/销毁工作,自然就卡顿了。给你几个靠谱的解决办法:

方案1:复用同一个全局accessoryView实例

如果所有cell的accessoryView完全一模一样,那咱们直接提前初始化好一个实例,之后给所有cell赋值的时候都用这个现成的就行,不用每次都重建。

示例代码:

// 在你的ViewController里声明一个属性
@property (nonatomic, strong) UIView *sharedAccessoryView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    
    // 只初始化这一次!
    self.sharedAccessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow_right"]];
    self.sharedAccessoryView.frame = CGRectMake(0, 0, 16, 16);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellID" forIndexPath:indexPath];
    
    // 直接用提前创建好的实例,不用再new了
    cell.accessoryView = self.sharedAccessoryView;
    
    // 其他cell内容配置...
    return cell;
}

方案2:在自定义Cell里一次性初始化accessoryView

如果你的cell是自定义的,那可以把accessoryView的初始化放在cell的初始化方法里——这样cell被复用的时候,accessoryView已经存在了,不用每次配置cell都重新创建。

示例代码:

// 自定义Cell类
@interface CustomTableViewCell : UITableViewCell
@end

@implementation CustomTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 这里只初始化一次,后续复用cell都用这个view
        UIImageView *accessoryImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow_right"]];
        accessoryImgView.frame = CGRectMake(0, 0, 16, 16);
        self.accessoryView = accessoryImgView;
    }
    return self;
}
@end

// 在ViewController里使用
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    // 注册自定义Cell
    [self.tableView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:@"CustomCellID"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCellID" forIndexPath:indexPath];
    
    // 这里不用再设置accessoryView了,初始化时已经搞定
    // 其他内容配置...
    return cell;
}

方案3:用系统自带的accessoryType代替自定义View

如果你的accessoryView只是简单的箭头、对勾这类常见样式,完全可以用系统提供的UITableViewCellAccessoryType,系统已经做了极致优化,绝对不会有卡顿问题:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellID" forIndexPath:indexPath];
    
    // 比如设置右侧箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    // 或者对勾:UITableViewCellAccessoryCheckmark
    
    // 其他配置...
    return cell;
}

如果试了这些还是卡顿,建议用Xcode的Time Profiler工具(Instruments里的)定位下具体耗时点,看看是不是还有其他主线程做耗时操作的地方。

内容的提问来源于stack exchange,提问作者Mick Ma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:38:45