2011-09-17 35 views
6

Tôi có một tableview với hai hành động trong nó, lần đầu tiên trên tôi sử dụng các delegate didSelectRowAtIndexPath, cho một thứ hai tôi muốn sử dụng một hành động trên một nút trên tế bào của tôi để làm cái gì khác. Vấn đề của tôi là tôi không thành công để có được đường dẫn chỉ mục? Cách tốt nhất để làm điều đó là gì.Làm thế nào để có được indexPath trong một tableview từ một hành động nút trong một tế bào?

+0

có thể trùng lặp ăn của [IOS 7 - Cách lấy indexPath từ nút được đặt trong UITableViewCell] (http://stackoverflow.com/questions/19000356/ios-7-how-to-get-the-indexpath-from-button-placed- in-uitableviewcell) –

Trả lời

9

Nếu bạn đã thêm vào nút để các tế bào như,

[cell.contentView addSubview:button]; 

sau đó, bạn có thể có được con đường chỉ số như,

- (void)onButtonTapped:(UIButton *)button { 

    UITableViewCell *cell = (UITableViewCell *)button.superview.superview; 
    NSIndexPath *indexPath = [tableView indexPathForCell:cell]; 
    // Go ahead 
} 
+0

Cảm ơn! Người gửi UIButton * button = (UIButton *); UITableViewCell * cell = (UITableViewCell *) button.superview.superview; –

+0

Được rồi! Chào mừng bạn! – EmptyStack

+1

Bạn không nên truy cập bảngView từ superview. Trong khi điều này hoạt động ngay bây giờ, nó có thể không hoạt động trên iOS mới. Ví dụ điển hình từ 6/7 Apple đã thêm một người giám sát khác. Điều này sẽ gặp sự cố – CW0007007

1

đây là mã đầy đủ cho một nút tùy chỉnh:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    CustomCellFilter *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (!cell) {cell = [[CustomCellFilter alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];} 

    //configure your cell here 
    cell.titleLabel.text = @"some title"; 

    //add button 
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    resetButton.frame = CGRectMake(40, 10, 18, 18); 
    [resetButton addTarget:self action:@selector(onButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
    [resetButton setImage:[UIImage imageNamed:@"someimage.png"] forState:UIControlStateNormal]; 
    [cell.contentView addSubview:myButton]; 

    //afterwards return the cell 
    return cell; 

- (void)onButtonTapped:(UIButton *)button { 

    UITableViewCell *cell = (UITableViewCell *)button.superview.superview; 
    NSIndexPath *indexPath = [filterTable indexPathForCell:cell]; 
    NSLog(@"%@", indexPath); 
    //use indexPath here... 
} 
-1

Trong trường hợp nút của bạn được di chuyển hoặc Apple thay đổi hệ thống phân cấp chế độ xem cho chế độ xem phụ kiện, phương pháp này sẽ mở rộng chuỗi để xem cho một UITableViewCell:

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

Tôi đang sử dụng giải pháp này - nhận con đường chỉ số của tế bào sử dụng điểm

CGPoint center= [sender center]; 
CGPoint rootViewPoint = [[sender superview] convertPoint:center toView:_tableView1]; 
NSIndexPath *indexPath = [_tableView1 indexPathForRowAtPoint:rootViewPoint]; 
NSLog(@"%@",indexPath); 

công việc của nó một cách hoàn hảo

+0

Điều này phù hợp hơn thay vì sử dụng 'superView.superView' – Mrug

0

Trong trường hợp bạn đang sử dụng một cái nhìn bộ sưu tập, bạn có thể sử dụng phương pháp Yogesh, nhưng thay đổi indexPathForRowAtPoint cho indexPathForItemAtPoint như sau:

CGPoint center= [sender center]; 
CGPoint rootViewPoint = [[sender superview] convertPoint:center toView:_tableView1]; 
NSIndexPath *indexPath = [_tableView1 indexPathForRowAtPoint:rootViewPoint]; 
NSLog(@"%@",indexPath); 
Các vấn đề liên quan