swift ios запускает индекс таблицы при нажатии кнопки - PullRequest
0 голосов
/ 26 апреля 2018

У меня есть кнопка в моей таблице, и когда она была нажата, она активирует индекс выбранной таблицы.например, когда я нажимаю кнопку с выбранным индексом, он запускает приведенный ниже код.Спасибо

код

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


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

Ответы [ 2 ]

0 голосов
/ 26 апреля 2018

попробуйте это: -

 @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)
        }
0 голосов
/ 26 апреля 2018

Используйте приведенный ниже код для получения индекса из TableViewCell:

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