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

Ngo*_*ern 3 ios watchkit wkinterfacetable

我必须在表格行中循环输出3个按钮,按下时它将重定向到相关的详细信息.

问题是如何识别用户点击的按钮?我曾尝试setAccessibilityLabel和setValue forKey,但都不起作用.

Hop*_*ust 5

您需要在CustomRow类中使用委托.

在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
Run Code Online (Sandbox Code Playgroud)

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

- (IBAction)buttonAction {
    [self.deleagte didSelectButton:self.button onCellWithIndex:self.index];
}
Run Code Online (Sandbox Code Playgroud)

在配置行的方法中的YourInterfaceController.m类中:

- (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;
    }
 }
Run Code Online (Sandbox Code Playgroud)

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

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