【发布时间】:2012-08-22 05:51:30
【问题描述】:
当表格视图大小发生变化时,基于视图的NSTableView 具有动态高度的行不会调整其行的大小。当行高来自表格视图的宽度时,这是一个问题(想想填充一列并换行从而扩展行大小的文本块)。
我一直在尝试让 NSTableView 在更改大小时调整其行的大小,但收效甚微:
- 如果我通过查询
enumerateAvailableRowViewsUsingBlock:仅调整可见行的大小,则某些不可见行不会调整大小,因此在用户滚动并显示这些行时以旧高度显示。 - 如果我调整所有行的大小,当有很多行时它会变得明显变慢(在我的 1.8Ghz i7 MacBook Air 中,每个窗口调整 1000 行后大约有 1 秒的延迟)。
有人可以帮忙吗?
这是我检测到表视图大小变化的地方 - 在表视图的委托中:
- (void)tableViewColumnDidResize:(NSNotification *)aNotification
{
NSTableView* aTableView = aNotification.object;
if (aTableView == self.messagesView) {
// coalesce all column resize notifications into one -- calls messagesViewDidResize: below
NSNotification* repostNotification = [NSNotification notificationWithName:BSMessageViewDidResizeNotification object:self];
[[NSNotificationQueue defaultQueue] enqueueNotification:repostNotification postingStyle:NSPostWhenIdle];
}
}
而以下是上面发布的通知的处理程序,其中可见行被调整大小:
-(void)messagesViewDidResize:(NSNotification *)notification
{
NSTableView* messagesView = self.messagesView;
NSMutableIndexSet* visibleIndexes = [NSMutableIndexSet new];
[messagesView enumerateAvailableRowViewsUsingBlock:^(NSTableRowView *rowView, NSInteger row) {
if (row >= 0) {
[visibleIndexes addIndex:row];
}
}];
[messagesView noteHeightOfRowsWithIndexesChanged:visibleIndexes];
}
调整所有行大小的替代实现如下所示:
-(void)messagesViewDidResize:(NSNotification *)notification
{
NSTableView* messagesView = self.messagesView;
NSIndexSet indexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,messagesView.numberOfRows)];
[messagesView noteHeightOfRowsWithIndexesChanged:indexes];
}
注意:这个问题与View-based NSTableView with rows that have dynamic heights 有点相关,但更侧重于响应表格视图的大小变化。
【问题讨论】:
标签: objective-c macos cocoa nstableview appkit