gpt4 book ai didi

swift - NSTableView 搜索后更新选定的单元格

转载 作者:行者123 更新时间:2023-11-30 13:51:03 25 4
gpt4 key购买 nike

我有 NSTableView 和一些数据。我想从 NSTableView 获取所选单元格的名称。当 View 加载时,我只需单击它即可。 Table shows correct name of the clicked cell当我搜索表格并单击搜索的单元格时,所选项目名称不会更新,它会显示搜索表格之前该位置的单元格的值。 enter image description here是谁能指出我在搜索表时如何更新数组?如有任何意见,将不胜感激。干杯。

下面的一些代码显示了如何导出所选单元格名称的 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包含过滤后的对象。如果您确实绑定(bind)了选择索引,则 menuController 的 selectedObjects 包含选定的对象。

关于swift - NSTableView 搜索后更新选定的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34257073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com