Detecting which UIButton was pressed in a UITableView

前端 未结 26 3351
小蘑菇
小蘑菇 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条回答
  •  猫巷女王i
    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];
    }
    

提交回复
热议问题