我有一个应用程序需要异步更新某些列中的单元格。我有一个 GCD 队列,其中添加了块,稍后获取给定的行和列的单元格,-rowForItem:
但结果-rowForItem:
始终为 0,并且NSAssert([self.outlineView itemAtRow:row] == item, @"rowForItem: and itemForRow: not reversible! (Item %@, row %ld)", item, (long)row);
每次都抛出。我看不出我在这里做错了什么。索引不是-1,这意味着没有找到该项目,但据说该项目位于不正确的索引处。(第一项始终是 JODirectoryInfo,我只在 JOFileInfo 上运行块,所以它不会是错误的项。)
- (NSTableCellView *)_cellForRow:(NSInteger)row columnIdentifier:(NSString *)column {
if (row < 0) return nil;
NSInteger colIdx = [self.outlineView columnWithIdentifier:column];
if (colIdx == -1) return nil;
if (colIdx + 1 > self.outlineView.tableColumns.count) return nil;
return [self.outlineView viewAtColumn:colIdx row:row makeIfNecessary:NO];
}
- (NSTableCellView *)_cellForItem:(id)item columnIdentifier:(NSString *)column {
NSInteger row = [self.outlineView rowForItem:item];
NSAssert([self.outlineView itemAtRow:row] == item, @"rowForItem: and itemForRow: not reversible! (Item %@, row %ld)", item, (long)row);
DDLogVerbose(@"Getting cell for item %@ (row %ld) in column %@", item, (long)row, column);
return [self _cellForRow:row columnIdentifier:column];
}
- (dispatch_block_t)_loadingBlockForItem:(JOFileInfo *)item block:(JOFileInfoBlock)block column:(NSString *)columnIdentifier {
return ^{
NSTableCellView *cellView = [self _cellForItem:item columnIdentifier:columnIdentifier];
if (!cellView) return;
cellView.objectValue = block(item);
};
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
DDLogVerbose(@"Requested value of column %@ for item %@", tableColumn.identifier, item);
if ([tableColumn.identifier isEqualToString:@"Size"]) {
if ([item isKindOfClass:[JOFileInfo class]]) {
dispatch_async(self.queue, [self _loadingBlockForItem:item block:^(JOFileInfo *item) {
unsigned long long size = item.size;
if (size != ULONG_LONG_MAX) return [self.byteCountFormatter stringFromByteCount:size];
else return @"--";
} column:tableColumn.identifier]);
return @"--";
} else {
return @"--";
}
} else {
return @"text";
}
}