我目前在单元格中定义了一个按钮和一个跟踪其UITouchDown操作的方法,如下所示:
- (void) clickedCallSign:(id)sender {
int index = [sender tag];
NSLog(@"event triggered %@",index);
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
//Callsign button
UIButton *button;
CGRect rect = CGRectMake(TEXT_OFFSET_X, BORDER_WIDTH, LABEL_WIDTH, LABEL_HEIGHT);
button = [[UIButton alloc] initWithFrame:rect];
cell.tag=[indexPath row];
button.tag=[indexPath row];
[button addTarget:self action:@selector(clickedCallSign:) forControlEvents:UIControlEventTouchDown];
[button setBackgroundColor:[UIColor redColor]];
[button setTitle:@"hello" forState:UIControlStateNormal];
[cell.contentView addSubview:button];
[button release];
}
然而,当我在模拟器中点击单元格时,控制台调试信息显示:“事件触发(null)”,我的应用程序稍后就会崩溃。
我该如何正确地将indexPath.row值传递到我的clickedCallSign方法中?