当按下按钮时,swift ios触发表索引

时间:2018-04-26 03:30:37

标签: ios swift selectedindex

我的表中有一个按钮,当它点击它时会触发所选的表索引。例如,当我单击所选索引处的按钮时,它将触发下面的代码。谢谢

func indexWasPressed(cell: ToDoListTableViewCell) {
 trigger it here
}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            print("You selected cell #\(indexPath.item)!")

2 个答案:

答案 0 :(得分:3)

使用以下代码从TableViewCell获取索引:

func indexWasPressed(cell: ToDoListTableViewCell) {
    let indexPath = yourTableView.indexPath(for: cell)
    let index = indexPath.row
} 

答案 1 :(得分:0)

试试这个: -

 @objc func buttonPressed(sender: UIButton) {
    // printing selected row
  print("index path: \(sender.tag)")

    //do you want section try this
   var section = Int()

    let pointInTable: CGPoint = sender.convert(sender.bounds.origin, to: self.tblView)
    let cellIndexPath = self.tblView.indexPathForRow(at: pointInTable)
    section(cellIndexPath!)
    section = cellIndexPath!.row
    print(section)


}

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

    print("cell for rows in \(indexPath.section)")


        let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifire", for: indexPath) as! yourcell

        cell.yourbutton.tag = indexpath.row

        cell.yourbutton.addTarget(self, action: #selector(buttonpressed(sender:)), for: UIControlEvents.touchUpInside)
        }
相关问题