2010-08-14 29 views
6

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?

+0

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

Trả lời

2

Trước hết, index là một int, vì vậy NSLog của bạn cần phải giống như thế này (chú ý %d):

NSLog(@"event triggered %d", index); 

(Đó là thể rằng điều này dẫn đến một vụ tai nạn, nhưng nó cũng có khả năng là một cái gì đó khác hoàn toàn là đi mà gây ra sự bất ổn.)

+0

'% d' đã thực hiện thủ thuật nhận giá trị indexPath.row vào clickCallSign :) ứng dụng vẫn gặp sự cố mặc dù .. với dấu vết ngăn xếp này: - [ UIButton setText:]: bộ chọn không được nhận dạng được gửi tới ví dụ 0x6877a10 2010-08-13 21: 48: 30.324 RF [8047: 207] *** Ter minating app do uncaught exception 'NSInvalidArgumentException', lý do: '- [UIButton setText:]: unrecognized selector được gửi tới instance 0x6877a10' – unicornherder

+0

Bạn có lẽ đã phát hành một thứ gì đó ở đâu đó. Việc quản lý bộ nhớ trong đoạn mã bạn hiển thị có vẻ ổn, vì vậy một số thứ khác không đúng. –

+1

thực sự .. tìm thấy giải pháp tôi cần ngay tại đây: http://developer.apple.com/iphone/library/samplecode/Accessory/Introduction/Intro.html Cảm ơn bạn đã giúp đỡ. Tôi rất trân trọng điều này. – unicornherder

2

Thẻ là tốt cho đến khi bạn không có cả hai phần và hàng. Hãy thử một cách khác để có được con đường chỉ số:

- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath { 

    //... 

    [button addTarget:self action:@selector(clickedCallSign:withEvent:) forControlEvents:UIControlEventTouchDown]; 

    //... 

} 

// Get the index path of the cell, where the button was pressed 
- (NSIndexPath*)indexPathForEvent:(id)event 
{ 
    NSSet *touches = [event allTouches]; 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentTouchPosition = [touch locationInView:self.tableView]; 
    return [self.tableView indexPathForRowAtPoint:currentTouchPosition]; 
} 

- (IBAction)clickedCallSign:(id)sender withEvent:(UIEvent*)event 
{ 
    NSIndexPath* buttonIndexPath = [self indexPathForEvent:event]; 
} 
2

Nếu bạn không muốn sử dụng các lĩnh vực thẻ, có các nút gọi phương pháp này:

- (void)tapAccessoryButton:(UIButton *)sender 
{ 
    UIView *parentView = sender.superview; 

    // the loop should take care of any changes in the view heirarchy, whether from 
    // changes we make or apple makes. 
    while (![parentView.class isSubclassOfClass:UITableViewCell.class]) 
     parentView = parentView.superview; 

    if ([parentView.class isSubclassOfClass:UITableViewCell.class]) { 
     UITableViewCell *cell = (UITableViewCell *) parentView; 
     NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
     [self tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath]; 
    } 
} 
Các vấn đề liên quan