How to get the indexpath.row when an element is activated?

前端 未结 19 2294
梦如初夏
梦如初夏 2020-11-21 23:30

I have a tableview with buttons and I want to use the indexpath.row when one of them is tapped. This is what I currently have, but it always is 0

var point =         


        
19条回答
  •  一向
    一向 (楼主)
    2020-11-22 00:03

    My approach to this sort of problem is to use a delegate protocol between the cell and the tableview. This allows you to keep the button handler in the cell subclass, which enables you to assign the touch up action handler to the prototype cell in Interface Builder, while still keeping the button handler logic in the view controller.

    It also avoids the potentially fragile approach of navigating the view hierarchy or the use of the tag property, which has issues when cells indexes change (as a result of insertion, deletion or reordering)

    CellSubclass.swift

    protocol CellSubclassDelegate: class {
        func buttonTapped(cell: CellSubclass)
    }
    
    class CellSubclass: UITableViewCell {
    
    @IBOutlet var someButton: UIButton!
    
    weak var delegate: CellSubclassDelegate?
    
    override func prepareForReuse() {
        super.prepareForReuse()
        self.delegate = nil
    }
    
    @IBAction func someButtonTapped(sender: UIButton) {
        self.delegate?.buttonTapped(self)
    }
    

    ViewController.swift

    class MyViewController: UIViewController, CellSubclassDelegate {
    
        @IBOutlet var tableview: UITableView!
    
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
            let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CellSubclass
    
            cell.delegate = self
    
            // Other cell setup
    
        } 
    
        //  MARK: CellSubclassDelegate
    
        func buttonTapped(cell: CellSubclass) {
            guard let indexPath = self.tableView.indexPathForCell(cell) else {
                // Note, this shouldn't happen - how did the user tap on a button that wasn't on screen?
                return
            }
    
            //  Do whatever you need to do with the indexPath
    
            print("Button tapped on row \(indexPath.row)")
        }
    } 
    

提交回复
热议问题