A view-based NSTableView
with rows that have dynamic heights doesn't resize its rows when the table view size is changed. This is a problem when the row height is derived from the table view's width (think text blocks that fills a column and wraps thus extending the row size).
I've been trying to get NSTableView
to resize its rows whenever it changed size but have experienced little success:
- If I resize only the visible rows by querying
enumerateAvailableRowViewsUsingBlock:
, some of the non-visible rows doesn't get resized and thus were shown with the old height when the user scrolls and reveal these rows. - If I resize all rows it becomes noticeably slow when there are a lot of rows (about 1 second delay after each window resize for 1000 rows in my 1.8Ghz i7 MacBook Air).
Anybody can help?
This is where I detect the table view size change - in the table view's delegate:
- (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];
}
}
Whereas the following is the handler of the notification posted above, where the visible rows get resized:
-(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];
}
The alternative implementation that resizes all rows looks like this:
-(void)messagesViewDidResize:(NSNotification *)notification
{
NSTableView* messagesView = self.messagesView;
NSIndexSet indexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,messagesView.numberOfRows)];
[messagesView noteHeightOfRowsWithIndexesChanged:indexes];
}
Note: This question is somewhat related to View-based NSTableView with rows that have dynamic heights but is more focused towards responding to the table view's size change.