Detecting which UIButton was pressed in a UITableView

前端 未结 26 2911
小蘑菇
小蘑菇 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: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; //:)
    }
    

提交回复
热议问题