WKInterfaceTable detect which row selected

2.1k views Asked by At

I just need to check which row was selected to pass data between WKInterfaceController. Here is my code, but NSLog doesn't show anything:

- (void)loadTableData {

     NSArray* items = [NSArray arrayWithObjects:@"cell 1 ",@"cell2 ", @"cell 3",@"cell4",nil];

    // Configure the table object (self.todoItems) and get the row controllers.
    [self.myTable setNumberOfRows:items.count withRowType:@"DinoNameRow"];
    NSInteger rowCount = self.myTable.numberOfRows;

    // Iterate over the rows and set the label for each one.
    for (NSInteger i = 0; i < rowCount; i++) {
        // Get the to-do item data.
        NSString* itemText = items[i];

        // Assign the text to the row's label.
        DinoNameRow* row = [self.myTable rowControllerAtIndex:i];
        [row.dinoName setText:itemText];
    }
}
- (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex {

    switch (rowIndex) {
        case 0:
            NSLog(@"ROW 1");
            break;

        default:
            break;
    }
}
2

There are 2 answers

3
hgwhittle On BEST ANSWER

Make sure that...

  1. Your WKInterfaceTable is connected via IBOutlet to your Storyboard element.
  2. Your rowType identifier for the row controller is set in Storyboard:enter image description here
  3. Your Class identifier for the row controller is set in Storyboard: enter image description here
  4. You have overridden the correct WKInterfaceController method:

    override func table(table: WKInterfaceTable, didSelectRowAtIndex rowIndex: Int) {
    
    }
    
1
Alex Cheng On

The code as below that I use in my project works well.

  override func table(table: WKInterfaceTable, didSelectRowAtIndex rowIndex: Int) {
        let string = data[rowIndex]
        self.delegate?.didSeletStyle(style, string: string, index: rowIndex)
        popController()
    }

  private func reloadTable() {
        for i in 0 ..< table.numberOfRows {
            let row = table.rowControllerAtIndex(i) as! tableRow
            row.label.setText(data[i])
        }
    }

private func getData(style: Style) {
    if style == .Friend {
        data = ["a","b","c","d"]
    }

    table.setNumberOfRows(data.count, withRowType: "tableRow")
    reloadTable()
}