Detecting which UIButton was pressed in a UITableView

前端 未结 26 2916
小蘑菇
小蘑菇 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:10

    Though I like the tag way... if you don't want to use tags for whatever reason, you could create a member NSArray of premade buttons:

    NSArray* buttons ;
    

    then create those buttons before rendering the tableView and push them into the array.

    Then inside of the tableView:cellForRowAtIndexPath: function you can do:

    UIButton* button = [buttons objectAtIndex:[indexPath row] ] ;
    [cell.contentView addSubview:button];
    

    Then in the buttonPressedAction: function, you can do

    - (void)buttonPressedAction:(id)sender {
       UIButton* button = (UIButton*)sender ;
       int row = [buttons indexOfObject:button] ;
       // Do magic
    }
    

提交回复
热议问题