Tôi hiện có một nút được định nghĩa trong một tế bào và một phương pháp để theo dõi hành động UITouchDown của nó như:UITableView Cell - nhận được IndexPath.row từ nút?
- (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];
}
Tuy nhiên khi tôi nhấp vào một tế bào trong mô phỏng, thông điệp điều khiển debug là: "sự kiện kích hoạt (null) "và ứng dụng của tôi bị lỗi ngay sau đó.
Làm cách nào tôi có thể nhận giá trị indexPath.row chính xác vào phương thức clickCallSign của tôi?
Xem giải pháp tốt hơn cho việc tìm kiếm indexPath từ nút ép: http://stackoverflow.com/a/16270198/308315 – iwasrobbed