如何在ios中轻拍单元附件时获取数据

问题描述 投票:0回答:4

我有tableview有几个单元格,当用户点击单元格时,它做了一些其他功能,当点击单元格配件,我希望它切换到另一个视图控制器我能够这样做,但问题是我无法发送单元格索引路径行的数据,为了发送数据我首先必须触摸单元格然后点击附件发送数据这里是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")
    }
}
ios swift segue cell accessory
4个回答
0
投票
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)
}

0
投票

感谢@shahzaib qureshi我删除了segue连接,并使用实例化视图控制器方法作为单元附件tapped方法中的详细视图控制器

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)
    print("ewwewew")
}

0
投票

只需传递sender参数中的索引路径,非常简单:

func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) { 
    performSegue(withIdentifier: "ShowDetail", sender: indexPath) 
} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
   if segue.identifier == "ShowDetail" { 
       let indexPath = sender as! IndexPath
       let svc = segue.destination as! DetailViewController
       svc.selectedObject = myCounters![indexPath.row]
   }
} 

0
投票

试试这个,

func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
        print("Index : \(indexPath.row)")

    var tempIndex: Int!
    var tempIdentifier: String!

    self.tempIndex = indexPath.row //You can store indexpath.row value in temp variable. And then then you can access it while performing any other functions.
    self.tempIdentifier = "ShowDetail" //This upto your needs.
}

// UIStoryboardSegue相应地移动另一个控制器。

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?[tempIndex])!
            }
        }
    default:
        print("error in segue")
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.