How can you get indexes of visible rows for an NSOutlineView?

和自甴很熟 提交于 2019-12-03 17:38:43

问题


How can you get a indexes of visible rows for an NSOutlineView? I need to know which level and which rows are visible.

[EDIT] What I'm actually looking for is an NSOutlineView equivalent to CocoaTouch/UITableView - (NSArray *)indexPathsForVisibleRows


回答1:


you can do the following:

NSScrollView* scrollView = [self.tableView enclosingScrollView];

CGRect visibleRect = scrollView.contentView.visibleRect;

NSRange range = [self.tableView rowsInRect:visibleRect];

in the range you will get the location as the first visible cell and in the length the amount of cells are shown so you can know the index of the visible cells.




回答2:


NSOutlineView is an NSTableView subclass. Therefore -rowsInRect: can be combined with -visibleRect (from NSView). Use -levelForRow: to determine the level.




回答3:


I changed my datasource to return an NSIndexPath for outlineView:child:ofItem:. This way I could use [outlineview rowAtPoint:point] (and similar) to get the NSIndexPath.

This change required me to make a set of these indexPaths so they wouldn't get released until I don't need them. Also, all the other code which normally expected a model object now needs to lookup the model object from the index path. In my case this was efficient.




回答4:


In Swift 3:

let rows = IndexSet(integersIn: 0..<outlineView.numberOfRows)
let rowViews = rows.flatMap { outlineView.rowView(atRow: $0, makeIfNecessary: false) as? RowView }

for rowView in rowViews 
    //iterate over rows
}


来源:https://stackoverflow.com/questions/4730926/how-can-you-get-indexes-of-visible-rows-for-an-nsoutlineview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!