简体   繁体   中英

NSTableView update selected cell after search

I have NSTableView with some data. I want to get the name of the selected cell from NSTableView. That works when view is loaded and I simply click on it. 该表显示了单击的单元格的正确名称 When I search through table and click on searched cell the selected item name doesn't update and it shows the value of the cell on that place before table was searched. 在此处输入图片说明 Is Can anyone point me to how to update the array when table is searched? Would appreciate any comments. Cheers.

Some code below showing how text2 variable for name of selected cell is derived:

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

}

arrangedObjects of menuController contains the filtered objects. selectedObjects of menuController contains the selected objects, if you did bind Selection Indexes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM