簡體   English   中英

在ios中點擊單元附件時如何獲取數據

[英]how to get data when cell accessory is tapped in ios

我有包含多個單元格的tableview,當用戶點擊該單元格時,它還具有其他功能,而當點擊了單元格附件時,我希望它能夠連接到另一個視圖控制器,我可以這樣做,但是問題是我無法發送單元格indexpath行中的數據,要發送數據,我首先必須觸摸該單元格,然后點擊附件以發送數據,這是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")
    }
}
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)
}

感謝@shahzaib qureshi,我刪除了segue連接,並在單元格附件輕按方法中使用實例化視圖控制器方法作為詳細視圖控制器

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")
}

只需在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]
   }
} 

嘗試這個,

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")
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM