How to get index path on click of button from UITableView in Swift 5 [duplicate]

大憨熊 提交于 2019-12-11 19:46:23

问题


In tableview, on the cells there are buttons in it, need to get the index of tableview means, which index cell's button is clicked. So, not able to implement it in swift 5. So far tried is but this give me wrong value.

let position: CGPoint = sender.convert(CGPoint.zero, to: organizationsTableView)
if let indexPath = organizationsTableView.indexPathForRow(at: position) {
    print(indexPath)
}

回答1:


1) In your Custom cell add closure:

class YoutuberTableViewCell: UITableViewCell {

    var buttonAction : (() -> ())?


    @IBAction func buttonTapped(_ sender: UIButton) {

        buttonAction?()
    }

}

2) In your cellForRowAt implement the closure

extension ViewController : UITableViewDataSource {

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath) as! YoutuberTableViewCell

        cell.buttonAction = { [unowned self] in
            let selectedIndexPath = indexPath.row
        }

        return cell
    }

}



回答2:


A callback closure is the most efficient way in Swift.

However only capturing the index path in cellForRowAt like in Prakash Shaiva's answer is not sufficient if cells can be inserted, deleted or moved. In this particular case you have to pass the cell

class MyTableViewCell: UITableViewCell {

    var callback : ((UITableViewCell) -> Void)?

    @IBAction func push(_ sender: UIButton) {
        callback?(self)
    }   
}

and get the current index path for the cell in the closure

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "MyIdentidier", for: indexPath) as! MyTableViewCell
    cell.callback = { currentCell in
        let currentIndexPath = tableView.indexPath(for: currentCell)!
    }
    return cell
}



回答3:


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {    
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
let ClickButton = cell.contentView.viewWithTag(1) as? UIButton
ClickButton?.tag = indexPath.row 
} 

Create a Button action and add your target with param Sender then you get value of indexPath with sender.tag




回答4:


add indexPath.row as a tag to the button inside tableViewCell

button.tag = indexPath.row
button.addTarget(self, action: #selector(buttonClickMethod(_:)), for: .touchUpInside)

If you are using xib then you can add tag to the button from Attribute Inspector

Inside buttonClickMethod you can access the index like this

func buttonClickMethod(_ sender: UIButton){
   let index = sender.tag
}


来源:https://stackoverflow.com/questions/58098923/how-to-get-index-path-on-click-of-button-from-uitableview-in-swift-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!