iosswiftwatchkitwkinterfacetable

didSelectRowAtIndex not being called for WKInterfaceTable


I have created a push segue in the storyboard from an WKInterfaceTableCell to another WKInterfaceController (called DetailInterfaceController).

When I tap on the row, didSelectRowAtIndex is not being called.

Does anybody know where I am going wrong, and how I can pass the string?

TableInterfaceController

didSelectRowAtIndexPath print statement is not called

@IBOutlet var table: WKInterfaceTable!
var objectsArray = ["1","2","3"]
var object: String!

override func table(table: WKInterfaceTable, didSelectRowAtIndex rowIndex: Int) {
    self.object = self.objectsArray[rowIndex]

    print(" object title: \(self.object)")
}


override func contextForSegueWithIdentifier(segueIdentifier: String) -> AnyObject? {
    // You may want to set the context's identifier in Interface Builder and check it here to make sure you're returning data at the proper times

    // Return data to be accessed in ResultsController
    return self.object
}

DetailsInterfaceController

The label is not set

@IBOutlet var label: WKInterfaceLabel!

override func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)

    // Make sure data was passed properly and update the label accordingly
    if let val: String = context as? String {
        self.label.setText(val)
    } else {
        self.label.setText("")
    }
    // Configure interface objects here.
}

Solution

  • Why it's not called:

    It's normal for table:didSelectRowAtIndex: to not be called since you used a storyboard segue. From the WKInterfaceController documentation:

    If you connected an action method to the table in your storyboard file, WatchKit does not call this method.

    How to pass the selected row data:

    You should use contextForSegueWithIdentifier:inTable:rowIndex: to return the context for the selected row:

    override func contextForSegueWithIdentifier(segueIdentifier: String, inTable table: WKInterfaceTable, rowIndex: Int) -> AnyObject? {
        if segueIdentifier == "someSegueIdentifier" {
            return objectsArray[rowIndex]
        }
        return nil
    }