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

时间:2015-12-13 22:20:24

标签: swift cocoa swift2 nstableview

我有一些数据的NSTableView。我想从NSTableView获取所选单元格的名称。这在加载视图时起作用,我只需单击它即可。 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

}

1 个答案:

答案 0 :(得分:0)

menuController的arrangeObjects包含过滤的对象。如果您确实绑定了选择索引,则menuController的selectedObjects包含所选对象。