我正在初始化一个简单的接口,其中一个 NSTableView 绑定到一个数组控制器(它管理一个字典数组)。我想在后台加载数组的内容(这是一个非常耗时的过程),每 100 或 1000 个元素更新一次表格视图。这个想法是界面可用且响应迅速。我不知道之后如何触发更新/刷新。桌子仍然是空的。任何人都可以提供指点吗?
我目前的做法是:
// In init for my app controller. This seems to work well, but I've tried other methods here.
[self performSelectorInBackground:@selector(loadTable) withObject:nil];
- (void)loadTable {
tracks = [[NSMutableArray alloc] initWithCapacity:[masters count]];
// ... create each object one-by-one. Add it to tracks.
for (... in ...) {
[tracks addObject:newObject];
}
// Now I don't know what to do next. The table remains empty.
// Things I've tried (though possibly not in all combinations with the
// method above):
// 1. With a suitably-defined reloadData method, which just reloads
// the table view and sets needs display.
[self performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
// 2. Reload directly.
[tv reloadData];
[tv setNeedsDisplay];
}
如果我只是直接加载数据,而不是在后台尝试这样做,一切正常,但需要将近 30 秒。