gpt4 book ai didi

objective-c - NSTableView : Can I lazily fetch subsets of the table datasource?

转载 作者:行者123 更新时间:2023-12-03 17:48:33 25 4
gpt4 key购买 nike

我可以延迟获取 NSTableView 数据源的子集吗?这样我就不需要将整个数据库加载到内存中?这样当表格滚动时,应用程序就会再次访问核心数据。

目前我只知道如何启用/禁用整个数据源的延迟抓取。但我想知道是否有办法在不同的运行中拆分抓取”。

最佳答案

在这种情况下,我最喜欢的方法如下。

我在 Controller 中保存了加载的记录数组。在 viewDidLoad 时,该数组包含零个元素。然后我从数据库请求第一部分记录(例如,受 50 条记录的获取限制限制)。获取记录后,我将它们放入这个数组中。还存储一个标志,指示所有记录是否已加载。

在我的 TableView 中,我总是显示数组中的所有记录。如果未加载所有记录,则我添加一个带有事件指示器的单元格。当用户将 TableView 向下滚动到带有事件指示器的单元格时,我请求从数据库加载下一部分记录(再次使用偏移量和获取限制)。加载记录后,我将它们添加到数组中,更新标志并在 tableView 上调用 reloadData。

我的 tableViewDataSource 的两个主要方法如下所示:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.allDataLoaded?self.dataArray.count?self.dataArray.count+1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row>=self.dataArray.count) {
// indicator cell
[self requestReadNext];
UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"IndicatorCell" forIndexPath:indexPath];
return cell;
} else {
MyCell* cell=[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
MyObject* obj=[self.dataArray objectAtIndex:indexPath.row];
[self fillCell:cell withObject:obj];
return cell;
}
}

关于objective-c - NSTableView : Can I lazily fetch subsets of the table datasource?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38179204/

25 4 0