Detecting which UIButton was pressed in a UITableView

前端 未结 26 2908
小蘑菇
小蘑菇 2020-11-22 00:40

I have a UITableView with 5 UITableViewCells. Each cell contains a UIButton which is set up as follows:

- (UITableView         


        
相关标签:
26条回答
  • 2020-11-22 01:04

    I would use the tag property like you said, setting the tag like so:

    [button setTag:indexPath.row];
    

    then getting the tag inside of the buttonPressedAction like so:

    ((UIButton *)sender).tag
    

    Or

    UIButton *button = (UIButton *)sender; 
    button.tag;
    
    0 讨论(0)
  • 2020-11-22 01:04

    Chris Schwerdt's solution but then in Swift worked for me:

    @IBAction func rateButtonTapped(sender: UIButton) {
        let buttonPosition : CGPoint = sender.convertPoint(CGPointZero, toView: self.ratingTableView)
        let indexPath : NSIndexPath = self.ratingTableView.indexPathForRowAtPoint(buttonPosition)!
    
        print(sender.tag)
        print(indexPath.row)
    }
    
    0 讨论(0)
  • 2020-11-22 01:06

    you can use the tag pattern:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         NSString *identifier = @"identifier";
         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
         if (cell == nil) {
             cell = [[UITableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
             [cell autorelelase];
    
             UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 5, 40, 20)];
             [button addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
             [button setTag:[indexPath row]]; //use the row as the current tag
             [cell.contentView addSubview:button];
    
             [button release];
         }
    
         UIButton *button = (UIButton *)[cell viewWithTag:[indexPath row]]; //use [indexPath row]
         [button setTitle:@"Edit" forState:UIControlStateNormal];
    
         return cell;
    }
    
    - (void)buttonPressedAction:(id)sender
    {
        UIButton *button = (UIButton *)sender;
        //button.tag has the row number (you can convert it to indexPath)
    }
    
    0 讨论(0)
  • 2020-11-22 01:06

    A slight variation on Cocoanuts answer (that helped me solve this) when the button was in the footer of a table (which prevents you from finding the 'clicked cell':

    -(IBAction) buttonAction:(id)sender;
    {
        id parent1 = [sender superview];   // UiTableViewCellContentView
        id parent2 = [parent1 superview];  // custom cell containing the content view
        id parent3 = [parent2 superview];  // UITableView containing the cell
        id parent4 = [parent3 superview];  // UIView containing the table
    
        UIView *myContentView = (UIView *)parent1;
        UITableViewCell *myTableCell = (UITableViewCell *)parent2;
        UITableView *myTable = (UITableView *)parent3;
        UIView *mainView = (UIView *)parent4;
    
        CGRect footerViewRect = myTableCell.frame;
        CGRect rect3 = [myTable convertRect:footerViewRect toView:mainView];    
    
        [cc doSomethingOnScreenAtY:rect3.origin.y];
    }
    
    0 讨论(0)
  • 2020-11-22 01:08
    func buttonAction(sender:UIButton!)
        {
            var position: CGPoint = sender.convertPoint(CGPointZero, toView: self.tablevw)
            let indexPath = self.tablevw.indexPathForRowAtPoint(position)
            let cell: TableViewCell = tablevw.cellForRowAtIndexPath(indexPath!) as TableViewCell
            println(indexPath?.row)
            println("Button tapped")
        }
    
    0 讨论(0)
  • 2020-11-22 01:08
    A better way would be to subclass your button and add a indexPath property to it.
    
    //Implement a subclass for UIButton.
    
    @interface NewButton:UIButton
    @property(nonatomic, strong) NSIndexPath *indexPath;
    
    
    Make your button of type NewButton in the XIB or in the code whereever you are initializing them.
    
    Then in the cellForRowAtIndexPath put the following line of code.
    
    button.indexPath = indexPath;
    
    return cell; //As usual
    
    
    
    Now in your IBAction
    
    -(IBAction)buttonClicked:(id)sender{
       NewButton *button = (NewButton *)sender;
    
    //Now access the indexPath by buttons property..
    
       NSIndexPath *indexPath = button.indexPath; //:)
    }
    
    0 讨论(0)
提交回复
热议问题