Detect tap on a button in UITableViewCell for UITableView containing multiple sections

前端 未结 4 1845
误落风尘
误落风尘 2020-12-04 16:00

I want to detect button tap on a UITableViewCell where the parent UITableView consists of multiple sections.

I was able to do it in the case of single section, but I

4条回答
  •  天命终不由人
    2020-12-04 16:46

    Objective-C

    -(void)addItem:(UIButton*) sender
    {
    
    CGPoint touchPoint = [sender convertPoint:CGPointZero toView:mainTable]; // maintable --> replace your tableview name
    NSIndexPath *clickedButtonIndexPath = [mainTable indexPathForRowAtPoint:touchPoint];
    
     NSLog(@"index path.section ==%ld",(long)clickedButtonIndexPath.section);
    
     NSLog(@"index path.row ==%ld",(long)clickedButtonIndexPath.row);
    
    
    }
    

    Swift3

     func addItem(sender: UIButton)
    {
        var touchPoint = sender.convert(CGPoint.zero, to: self.maintable)
        // maintable --> replace your tableview name
        var clickedButtonIndexPath = maintable.indexPathForRow(at: touchPoint)
        NSLog("index path.section ==%ld", Int(clickedButtonIndexPath.section))
        NSLog("index path.row ==%ld", Int(clickedButtonIndexPath.row))
    
    
    }
    

    Swift2 and above

    func addItem(sender: UIButton)
     {
    var touchPoint: CGPoint = sender.convertPoint(CGPointZero, toView: mainTable)
        // maintable --> replace your tableview name
    var clickedButtonIndexPath: NSIndexPath = mainTable(forRowAtPoint: touchPoint)
    NSLog("index path.section ==%ld", Int(clickedButtonIndexPath.section))
    NSLog("index path.row ==%ld", Int(clickedButtonIndexPath.row))
    }
    

提交回复
热议问题