2017-08-09 73 views
1

我有一個NSTableView並在每個錶行內有一個NSTextView。我在故事板中禁用了滾動NSTextView。我可以滾動我的NSTableView就好了,但是當我的鼠標光標位於NSTextView的頂部時,滾動停止。我認爲這是因爲NSTextView正在攔截滾動行爲。NSTextView裏面的NSTableView塊滾動

箭頭指向NSTextView

Arrow points to the NSTextView

請記住,NSTextView是在子類NSTableViewCell

class AnnouncementsVC: NSViewController, NSTableViewDelegate, NSTableViewDataSource { 
    @IBOutlet weak var tableView: NSTableView! 

    override func viewDidLoad() { 
    //... 
    } 
    func numberOfRows(in tableView: NSTableView) -> Int { 
    //... 
    } 
    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { 
    //... 
    } 
} 

class AnnouncementTableCell: NSTableCellView{ 
    @IBOutlet weak var message: NSTextView! 
} 

如何讓NSTextView通過其滾動的事件多達其父NSTableView?我的猜測是我需要scrollWheel(with event: NSEvent),但我不確定它在哪裏,因爲我在這裏有兩個不同的課程。實現這一

+0

你需要'NSTextView'還是可以使用多行'NSTextField'? – Willeke

+0

我選擇了一個'NSTextView',以便我可以調整行高。我想我可以使用'NSTextField'並使用一個屬性字符串。 –

回答

0

一種方法是子類NSScrollView和實施scrollWheel:有條件(1)無論是消耗在你的子類的實例滾動事件需要,或(2)將它們轉發到一個封閉的滾動型,根據你的情況下, '當前的滾動位置。

這裏是一個相當基本的實現(OBJ - C的),我已經在過去使用:

- (void)scrollWheel:(NSEvent *)e { 


    if(self.enclosingScrollView) { 

     NSSize 
     contSize = self.contentSize, 
     docSize = [self.documentView bounds].size, 
     scrollSize = NSMakeSize(MAX(docSize.width - contSize.width, 0.0f), 
           MAX(docSize.height - contSize.height,0.0f)); 

     NSPoint 
     scrollPos = self.documentVisibleRect.origin, 
     normPos = NSMakePoint(scrollSize.width ? scrollPos.x/scrollSize.width : 0.0, 
           scrollSize.height ? scrollPos.y/scrollSize.height : 0.0); 

     if(((NSView*)self.documentView).isFlipped) normPos.y = 1.0 - normPos.y; 

     if( (e.scrollingDeltaX>0.0f && normPos.x>0.0f) 
      || (e.scrollingDeltaX<0.0f && normPos.x<1.0f) 
      || (e.scrollingDeltaY<0.0f && normPos.y>0.0f) 
      || (e.scrollingDeltaY>0.0f && normPos.y<1.0f)) { 

      [super scrollWheel:e]; 

     } 
     else 

      [self.nextResponder scrollWheel:e]; 

    } 
    else 
     // Don't bother when not nested inside another NSScrollView 
     [super scrollWheel:e]; 
} 

它仍然極不理想,像independantly處理DELTAX和移動deltaY成分多,但也許這是足夠你的情況。