WKInterfaceTable how to identify button on each row?
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 to share