2012-08-22 81 views
9

具有動態高度的行的基於視圖的NSTableView不會在更改表視圖大小時調整行的大小。當行高從表視圖的寬度派生時(認爲填充列和換行從而擴展行大小的文本塊),這是一個問題。在基於視圖的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有些相關,但更注重響應表視圖的大小更改。

回答

11

我剛剛經歷了這個確切的問題。我所做的就是監控NSViewBoundsDidChangeNotification滾動視圖的內容視圖

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollViewContentBoundsDidChange:) name:NSViewBoundsDidChangeNotification object:self.scrollView.contentView]; 

並在處理程序,獲得可見的列和調用noteHeightOfRowsWithIndexesChange :.我禁用動畫,而這樣做,因此用戶不會看到調整大小時行扭動作爲視圖的進入表

- (void)scrollViewContentBoundsDidChange:(NSNotification*)notification 
{ 
    NSRange visibleRows = [self.tableView rowsInRect:self.scrollView.contentView.bounds]; 
    [NSAnimationContext beginGrouping]; 
    [[NSAnimationContext currentContext] setDuration:0]; 
    [self.tableView noteHeightOfRowsWithIndexesChanged:[NSIndexSet indexSetWithIndexesInRange:visibleRows]]; 
    [NSAnimationContext endGrouping]; 
} 

這有快速執行得這麼好表滾動,但它的工作對我非常好。

+0

對我來說,我一直等到用戶完成調整大小,然後重新調整行高。 – adib

+1

顯然滾動視圖內容視圖不會發布更改通知,即使在調用'[self.scrollView.contentView setPostsBoundsChangedNotifications:YES]' – adib

+1

似乎NSViewFrameDidChangNotification更適合實時調整大小 –

相關問題