代码之家  ›  专栏  ›  技术社区  ›  dpigera

uiTableView单元格-是否从按钮获取indexPath.row?

  •  6
  • dpigera  · 技术社区  · 16 年前

    我目前在一个单元格中定义了一个按钮,并有一个方法来跟踪它的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];   
    }
    

    但是,当我单击模拟器中的一个单元时,控制台调试消息是:“事件触发(空)”,我的应用程序很快就会崩溃。

    如何将indexPath.row值正确获取到ClickedCallSign方法中?

    3 回复  |  直到 12 年前
        1
  •  2
  •   Ben Zotto sberry    16 年前

    首先, index 是一个 int ,因此您的nslog需要如下所示(请注意 %d ):

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

    (这是 可能的 这会导致撞车,但也有可能是其他事情导致了不稳定。)

        2
  •  2
  •   JastinBall    12 年前

    在没有节和行之前,标记是很好的。尝试其他方法获取索引路径:

    - (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];
    }
    
        3
  •  2
  •   mahboudz    12 年前

    如果不想使用标记字段,请让按钮调用此方法:

    - (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];
        }
    }
    
    推荐文章