NSTextView里面的NSTableView块滚动
问题描述:
我有一个NSTableView
并在每个表行内有一个NSTextView
。我在故事板中禁用了滚动NSTextView
。我可以滚动我的NSTableView
就好了,但是当我的鼠标光标位于NSTextView
的顶部时,滚动停止。我认为这是因为NSTextView
正在拦截滚动行为。NSTextView里面的NSTableView块滚动
箭头指向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)
,但我不确定它在哪里,因为我在这里有两个不同的课程。实现这一
答
一种方法是子类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成分多,但也许这是足够你的情况。
你需要'NSTextView'还是可以使用多行'NSTextField'? – Willeke
我选择了一个'NSTextView',以便我可以调整行高。我想我可以使用'NSTextField'并使用一个属性字符串。 –