MST

星途 面试题库

面试题:Objective-C中表格视图优化之数据源处理

在Objective-C开发的应用中,表格视图(UITableView)频繁出现性能问题。假设表格视图的数据源是一个庞大的数组,数组中每个元素是复杂的数据对象。请描述一种优化数据源处理的方法,以提升表格视图的性能,并编写关键代码示例。
40.4万 热度难度
编程语言Objective-C

知识考点

AI 面试

面试题答案

一键面试

优化方法

  1. 数据分批加载:只加载当前屏幕可见及附近的部分数据,而不是一次性加载整个庞大数组。
  2. 数据复用:利用UITableView的单元格复用机制,减少频繁创建新的单元格。
  3. 异步处理:在后台线程处理复杂数据对象的某些计算,避免阻塞主线程。

关键代码示例

  1. 数据分批加载与复用
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
// 假设这里是庞大数组,但只取部分展示
@property (nonatomic, strong) NSArray *partialDataArray; 
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, assign) NSInteger pageSize;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.currentPage = 0;
    self.pageSize = 20;
    
    // 初始化表格视图
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    
    // 模拟获取庞大数组
    NSArray *hugeArray = [self generateHugeArray]; 
    [self loadDataForPage:self.currentPage];
}

// 模拟生成庞大数组
- (NSArray *)generateHugeArray {
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 0; i < 1000; i++) {
        // 假设每个元素是复杂数据对象
        ComplexDataObject *obj = [[ComplexDataObject alloc] initWithData:i]; 
        [array addObject:obj];
    }
    return array;
}

- (void)loadDataForPage:(NSInteger)page {
    NSArray *hugeArray = [self generateHugeArray];
    NSInteger startIndex = page * self.pageSize;
    NSInteger endIndex = MIN((page + 1) * self.pageSize, hugeArray.count);
    self.partialDataArray = [hugeArray subarrayWithRange:NSMakeRange(startIndex, endIndex - startIndex)];
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.partialDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    ComplexDataObject *obj = self.partialDataArray[indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@", obj.data];
    return cell;
}
  1. 异步处理(假设复杂数据对象有需要计算的属性)
@interface ComplexDataObject : NSObject

@property (nonatomic, assign) NSInteger data;
@property (nonatomic, strong) NSString *computedString;

- (instancetype)initWithData:(NSInteger)data;

@end

@implementation ComplexDataObject

- (instancetype)initWithData:(NSInteger)data {
    self = [super init];
    if (self) {
        self.data = data;
        // 异步计算复杂属性
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSString *computed = [self performComplexCalculation];
            self.computedString = computed;
        });
    }
    return self;
}

- (NSString *)performComplexCalculation {
    // 模拟复杂计算
    sleep(1); 
    return [NSString stringWithFormat:@"Computed: %ld", (long)self.data];
}
@end

在实际应用中,还可以结合缓存机制,避免重复计算复杂数据对象的某些属性,进一步提升性能。同时,根据滚动事件动态加载下一页数据,确保用户体验流畅。