WKInterfaceTable如何识别每一行中的按钮?

WKInterfaceTable how to identify button in each of the row?

我不得不循环出 table 行中的 3 个按钮,按下时它将重定向到相关详细信息。

问题是如何识别用户点击了哪个按钮?我试过 setAccessibilityLabelsetValue forKey 但都不起作用。

尝试像这样将 (sender: UIButton) 放入您的 IBAction 中:

@IBAction 功能按钮已按下(发件人:UIButton)

如果按下按钮,WatchKit 将调用以下方法:

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

使用 rowIndex 参数来决定应该执行哪个操作。

您需要在 CustomRow Class.

中使用 delegate

CustomRow.h文件中:

@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

CustomRow.m 文件中,您需要添加 IBAction 连接到 IB 中的按钮。然后处理这个动作:

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

YourInterfaceController.m class 中配置行的方法中:

- (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;
    }
 }

现在您只需实施您的 委托 方法:

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