Detecting which UIButton was pressed in a UITableView

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

    Note here i am using custom cell this code is perfectly working for me

     @IBAction func call(sender: UIButton)
        {
            var contentView = sender.superview;
            var cell = contentView?.superview as EmployeeListCustomCell
            if (!(cell.isKindOfClass(EmployeeListCustomCell)))
            {
                cell = (contentView?.superview)?.superview as EmployeeListCustomCell
            }
    
            let phone = cell.lblDescriptionText.text!
            //let phone = detailObject!.mobile!
            let url:NSURL = NSURL(string:"tel://"+phone)!;
            UIApplication.sharedApplication().openURL(url);
        }
    
    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
提交回复
热议问题