iosswiftseguecellaccessory

how to get data when cell accessory is tapped in ios


i have tableview which has several cells, when user tap on the cell it does some other function and when cell accessory is tapped i want it to segue to another view controller i am able to do so but the problem is i am not able to send the data at cell indexpath row, in order to send data i first have to touch the cell then tap on accessory to send the data here is the code for tableview

func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
    myTableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    switch segue.identifier {
    case "ShowDetail":
        if let ip = myTableView.indexPathForSelectedRow{
            if let svc = segue.destination as? DetailViewController{
                svc.selectedObject = (myCounters?[ip.row])!
            }
        }
    default:
        print("error in segue")
    }
}

Solution

  • func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
        let vc = storyboard?.instantiateViewController(withIdentifier: "detailvc") as? DetailViewController
    vc?.selectedObject = (myCounters?[indexPath.row])!
    navigationController?.present(vc!, animated: true, completion: nil)
    }