NSTableView 在搜索后更新选定的单元格
NSTableView update selected cell after search
我有 NSTableView 和一些数据。我想从 NSTableView 中获取所选单元格的名称。这在加载视图时有效,我只需单击它即可。
当我搜索 table 并单击搜索到的单元格时,所选项目名称不会更新,它会显示搜索 table 之前该位置的单元格值。
是
谁能告诉我如何在搜索 table 时更新数组?将不胜感激任何意见。干杯。
下面的一些代码显示了如何导出所选单元格名称的 text2 变量:
var menuItems = [mainMenuData(calculationType: "Steel", calculationName: "Simply supported beam", calculationtoCode: "EN1993, BS5950"),
mainMenuData(calculationType: "Concrete", calculationName: "Pad Foundations", calculationtoCode: "EN1992, BS8110"),
mainMenuData(calculationType: "Loading", calculationName: "Wind loading", calculationtoCode: "EN1994, BS5675"),
mainMenuData(calculationType: "Timber", calculationName: "timber rafter", calculationtoCode: "EN1995, BS3453"),
mainMenuData(calculationType: "Steel", calculationName: "Portal Frame analysis", calculationtoCode: "EN1993, BS5950"),
mainMenuData(calculationType: "Foundations", calculationName: "Pad foundations", calculationtoCode: "EN1992, BS8110")]
@IBOutlet var menuController: NSArrayController!
@IBOutlet weak var statusLabel: NSTextField!
@IBOutlet weak var mainMenuTableView: NSTableView!
@IBOutlet weak var selectedCalc: NSTextField!
func updateMainMenu(){
let text : String
let text2 : String
let itemSelected = mainMenuTableView.selectedRowIndexes.count
let selected = mainMenuTableView.selectedRow
if (itemSelected == 0) {
text = "\(menuItems.count) items"
text2 = "nothing selected"
}
else
{
text = "\(itemSelected) of \(menuItems.count) selected"
text2 = "selected \(menuItems[selected].calculationName)"
}
statusLabel.stringValue = text
selectedCalc.stringValue = text2
}
func tableViewSelectionDidChange(notification: NSNotification) {
updateMainMenu()
}
@IBAction func searchChange(sender: AnyObject) {
let searchString = sender.stringValue
var predicate: NSPredicate? = nil
if (searchString != ""){
var dict: [String : AnyObject] = ["value" : searchString]
predicate = searchPredicate.predicateWithSubstitutionVariables(dict)
}
menuController.filterPredicate = predicate
}
menuController 的 arrangedObjects 包含过滤后的对象。 menuController 的 selectedObjects 包含选定的对象,如果您确实绑定了选择索引。
我有 NSTableView 和一些数据。我想从 NSTableView 中获取所选单元格的名称。这在加载视图时有效,我只需单击它即可。
下面的一些代码显示了如何导出所选单元格名称的 text2 变量:
var menuItems = [mainMenuData(calculationType: "Steel", calculationName: "Simply supported beam", calculationtoCode: "EN1993, BS5950"),
mainMenuData(calculationType: "Concrete", calculationName: "Pad Foundations", calculationtoCode: "EN1992, BS8110"),
mainMenuData(calculationType: "Loading", calculationName: "Wind loading", calculationtoCode: "EN1994, BS5675"),
mainMenuData(calculationType: "Timber", calculationName: "timber rafter", calculationtoCode: "EN1995, BS3453"),
mainMenuData(calculationType: "Steel", calculationName: "Portal Frame analysis", calculationtoCode: "EN1993, BS5950"),
mainMenuData(calculationType: "Foundations", calculationName: "Pad foundations", calculationtoCode: "EN1992, BS8110")]
@IBOutlet var menuController: NSArrayController!
@IBOutlet weak var statusLabel: NSTextField!
@IBOutlet weak var mainMenuTableView: NSTableView!
@IBOutlet weak var selectedCalc: NSTextField!
func updateMainMenu(){
let text : String
let text2 : String
let itemSelected = mainMenuTableView.selectedRowIndexes.count
let selected = mainMenuTableView.selectedRow
if (itemSelected == 0) {
text = "\(menuItems.count) items"
text2 = "nothing selected"
}
else
{
text = "\(itemSelected) of \(menuItems.count) selected"
text2 = "selected \(menuItems[selected].calculationName)"
}
statusLabel.stringValue = text
selectedCalc.stringValue = text2
}
func tableViewSelectionDidChange(notification: NSNotification) {
updateMainMenu()
}
@IBAction func searchChange(sender: AnyObject) {
let searchString = sender.stringValue
var predicate: NSPredicate? = nil
if (searchString != ""){
var dict: [String : AnyObject] = ["value" : searchString]
predicate = searchPredicate.predicateWithSubstitutionVariables(dict)
}
menuController.filterPredicate = predicate
}
menuController 的 arrangedObjects 包含过滤后的对象。 menuController 的 selectedObjects 包含选定的对象,如果您确实绑定了选择索引。