【发布时间】:2022-03-19 02:35:23
【问题描述】:
我对如何在 searchField 的文本更改时重新加载 NSTableView 的数据感到有些困惑。我假设我需要根据字符串匹配和tableView.reloadData() 内部的所有controlTextDidChange() 调用过滤tableView 用来填充自身的数组,但是当我测试代码时它不起作用。我假设这将是一个非常简单的修复,并且我在这个主题上找到的大部分信息都有些复杂。所以我不知道解决方案到底有多简单或复杂
这是我的controlTextDidChange() 函数:
func controlTextDidChange(_ obj: Notification) {
noteRecords.filter { $0.title.contains(searchField.stringValue) }
tableView.reloadData()
}
如果有人可以解释如何在 searchField 的文本更改时使用过滤后的数组重新加载 tableViews 数据,那将非常感激。另外,如果用普通的 NSTextField 替换 searchField 更容易,请告诉我。
编辑
这是填充 tableView 的函数:
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
let note = allRecords[row]
let cellIdentifier = NSUserInterfaceItemIdentifier(rawValue: \"NoteInCloudCell\")
if tableColumn == tableView.tableColumns[0] {
let cell = tableView.makeView(withIdentifier: cellIdentifier, owner: nil) as? NoteInCloudCell
cell?.noteTitle.stringValue = note.title
cell?.cloudID.stringValue = note.cloudID
return cell
}
else { return nil }
}
标签: swift nstableview nstextfield nssearchfield