macos - NSTableView hideRows 在旧行曾经所在的位置留下空白空间

标签 macos cocoa nstableview appkit

我正在研究当用户点击按钮时隐藏和显示行的能力。当我调用 NSTableView.hideRows 时,行会按预期动画消失,但 tableView 不会随之拉动剩余的行,因此在tableView 的中间。如果我点击附加行,则会看到剩余行被拉起的动画。

下面显示的图像...

包含所有行的表格:

Table with all rows

显示的差距:

enter image description here

代码:

  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 ) 函数存在错误,但您可以通过执行以下操作来解决此问题。

  1. 调用tableView.hideRows(at)
  2. 调用tableView.noteHeightOfRows(withIndexesChanged: range)
  3. 将这段代码添加到您的 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/

相关文章:

objective-c - 如何避免图像重定位使用带有 HTML 内容的 NSSharingService 发送电子邮件

iphone - 如何在 Cocoa Touch 中验证网站证书?

objective-c - 使用从 textFields 创建的对象填充 NSTableView

swift - 如何更改 NSTableHeaderCell 的字体大小

c++ - 在 Mac 终端中运行 OpenCV 3

macos - 如何为 Mac 安装 python3.4-dev?

python - Mac上的python安装可以强制使用tcl/tk 8.6吗?

cocoa - 设置隐藏不起作用

Cocoa:什么创建链接到 NIB 文件的 Controller 类的实例?

cocoa - NSTableView,NSArrayController 并仅在按键后重新加载?