我正在尝试使用 创建 UI 元素NSBrowser
,但由于某种原因,浏览器会创建我指定的自定义单元格类的单例实例,而不是为浏览器中的每个单元格创建新的单元格实例。特别是,这是委托实现
- (void)awakeFromNib {
[super awakeFromNib];
self.browser.target = self;
self.browser.doubleAction = @selector(_doubleClick:);
self.browser.reusesColumns = YES;
// self.browser.cellClass = [CLColumnViewCell class];
[self.browser setCellPrototype:[[CLColumnViewCell alloc] init]];
}
#pragma mark NSBrowserDelegate
- (void)browser:(NSBrowser *)browser createRowsForColumn:(NSInteger)column inMatrix:(NSMatrix *)matrix {
NSLog(@"This method never gets called");
}
- (NSInteger)browser:(NSBrowser *)browser numberOfChildrenOfItem:(id)item {
return [(item ?: _root) countOfContents];
}
- (BOOL)browser:(NSBrowser *)browser isLeafItem:(id)item {
return ![(item ?: _root) canHaveContents];
}
- (id)browser:(NSBrowser *)browser child:(NSInteger)index ofItem:(id)item {
return [(item ?: _root) objectInContentsAtIndex:index];
}
- (id)browser:(NSBrowser *)browser objectValueForItem:(id)item {
return nil; // Object value is handled by CLColumnViewCell itself.
}
- (void)browser:(NSBrowser *)browser willDisplayCell:(CLColumnViewCell *)cell atRow:(NSInteger)row column:(NSInteger)column {
NSIndexPath *indexPath = [[browser indexPathForColumn:column] indexPathByAddingIndex:row];
cell.columnView = browser;
cell.indexPath = indexPath;
cell.object = [browser itemAtIndexPath:indexPath];
NSLog(@"will display cell %@", cell, cell);
}
日志消息如下所示
2012-11-11 09:57:16.582 will diplay cell <CLColumnViewCell: 0x102e597c0>
2012-11-11 09:57:16.583 will diplay cell <CLColumnViewCell: 0x102e597c0>
2012-11-11 09:57:16.583 will diplay cell <CLColumnViewCell: 0x102e597c0>
2012-11-11 09:57:16.584 will diplay cell <CLColumnViewCell: 0x102e597c0>
2012-11-11 09:57:16.584 will diplay cell <CLColumnViewCell: 0x102e597c0>
2012-11-11 09:57:16.585 will diplay cell <CLColumnViewCell: 0x102e597c0>
2012-11-11 09:57:16.585 will diplay cell <CLColumnViewCell: 0x102e597c0>
2012-11-11 09:57:16.586 will diplay cell <CLColumnViewCell: 0x102e597c0>
2012-11-11 09:57:16.587 will diplay cell <CLColumnViewCell: 0x102e597c0>
2012-11-11 09:57:16.587 will diplay cell <CLColumnViewCell: 0x102e597c0>
2012-11-11 09:57:16.588 will diplay cell <CLColumnViewCell: 0x102e597c0>
2012-11-11 09:57:16.588 will diplay cell <CLColumnViewCell: 0x102e597c0>
2012-11-11 09:57:16.589 will diplay cell <CLColumnViewCell: 0x102e597c0>
2012-11-11 09:57:16.589 will diplay cell <CLColumnViewCell: 0x102e597c0>
正如消息所示,同一个单元格实例被一遍又一遍地使用!有没有办法强制NSBrowser
创建新的单元格实例而不是重用同一个?