WKInterfaceTable how to identify button on each row?

I had three loops in a table row and will redirect to related parts when clicked.

The problem is how to determine which button to press the button? I've tried setAccessibilityLabel

and setValue forKey

, but both don't work.

+3


source to share


3 answers


You need to use a delegate in the CustomRow class .

In the CustomRow.h file :

@protocol CustomRowDelegate;

@interface CustomRow : NSObject
@property (weak, nonatomic) id <CustomRowDelegate> deleagte;

@property (assign, nonatomic) NSInteger index;

@property (weak, nonatomic) IBOutlet WKInterfaceButton *button;
@end

@protocol CustomRowDelegate <NSObject>

- (void)didSelectButton:(WKInterfaceButton *)button onCellWithIndex:(NSInteger)index;

@end

      

In your CustomRow.m file , you need to add an IBAction connected to your button in IB. Then handle this action:

- (IBAction)buttonAction {
    [self.deleagte didSelectButton:self.button onCellWithIndex:self.index];
}

      



In the YourInterfaceController.m class , in the method where you are setting up the strings:

- (void)configureRows {

    NSArray *items = @[*anyArrayWithData*];

    [self.tableView setNumberOfRows:items.count withRowType:@"Row"];
    NSInteger rowCount = self.tableView.numberOfRows;

    for (NSInteger i = 0; i < rowCount; i++) {

        CustomRow* row = [self.tableView rowControllerAtIndex:i];

        row.deleagte = self;
        row.index = i;
    }
 }

      

Now you need to implement your delegation method:

- (void)didSelectButton:(WKInterfaceButton *)button onCellWithIndex:(NSInteger)index {
     NSLog(@" button pressed on row at index: %d", index);
}

      

+5


source


If you click the button, WatchKit will call the following method:

- (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex

      



Use the rowIndex parameter to decide what action should be taken.

+2


source


try putting (sender: UIButton) in your IBAction like this:

@IBAction funcPressed button (sender: UIButton)

0


source







All Articles