我正在研究当用户点击按钮时隐藏和显示行的能力。当我调用 NSTableView.hideRows 时,行会按预期动画消失,但 tableView 不会随之拉动剩余的行,因此在tableView
的中间。如果我点击附加行,则会看到剩余行被拉起的动画。
下面显示的图像...
包含所有行的表格:
显示的差距:
代码:
func numberOfRows(in tableView: NSTableView) -> Int {
return 50
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
if let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier.testCell, owner: nil) as? TestCell {
cell.label.stringValue = String(row)
return cell
}
return nil
}
func tableViewSelectionDidChange(_ notification: Notification) {
if let tableViewObject = notification.object as? NSTableView {
let index = tableViewObject.selectedRow
if index >= 0 {
self.tableView.hideRows(at: IndexSet(arrayLiteral: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), withAnimation: .slideUp)
}
}
}
有什么我错过的吗?
Git 链接 https://gitlab.com/RollingGoron/broken-nstable
****************更新********************
在 hideRows
之后调用 self.tableView.noteHeightOfRows(withIndexesChanged: range)
修复了我的示例项目中的问题,但如果您尝试隐藏则无法解决问题屏幕外的行。
例如如果您尝试隐藏 100 行,它会将它们动画化,但不会留下 100 行间隙,而是根据行的大小留下约 50 行的间隙。
我很想看看是否可以隐藏当前不在屏幕上的行。
最佳答案
好的,我解决了。
我认为 NSTableView 中的 hideRows(at ) 函数存在错误,但您可以通过执行以下操作来解决此问题。
- 调用tableView.hideRows(at)
- 调用tableView.noteHeightOfRows(withIndexesChanged: range)
将这段代码添加到您的
tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView?
if tableView.hiddenRowIndexes.contains(row) { return nil }
这将确保 tableView 在动画消失时不会尝试绘制行。
关于macos - NSTableView hideRows 在旧行曾经所在的位置留下空白空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55765958/