首页 > 解决方案 > 按下 UITableView 行时正确触发视图控制器打开的问题?

问题描述

UITableView当按下一行时,如何正确触发视图控制器打开?

我需要允许用户从视图控制器返回到 tableView 并允许他们选择相同或不同的 tableView 行。

我目前遇到的问题是应用程序在从选择其中一行时打开的 ViewController 返回后多次选择同一行时崩溃:scheduledDelivery

目前,这是我拥有的代码:

import UIKit

class ScheduledCell: UITableViewCell {

    @IBOutlet weak var ETALabel: UILabel!
    @IBOutlet weak var cellStructure: UIView!

    @IBOutlet weak var scheduledLabel: UILabel!
    @IBOutlet weak var testingCell: UILabel!
    @IBOutlet weak var pickupLabel: UILabel!
    @IBOutlet weak var deliveryLabel: UILabel!
    @IBOutlet weak var stopLabel: UILabel!
    @IBOutlet weak var topBar: UIView!


}

class ToCustomerTableViewController: UITableViewController, UIGestureRecognizerDelegate {

    var typeValue = String()

    var driverName = UserDefaults.standard.string(forKey: "name")!
    var structure = [AlreadyScheduledStructure]()


    override func viewDidLoad() {
        super.viewDidLoad()

        fetchJSON()


        //Disable delay in button tap
        self.tableView.delaysContentTouches = false

        tableView.tableFooterView = UIView()

    }


    private func fetchJSON() {
        guard let url = URL(string: "https://example.com/example/example"),
            let value = driverName.addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed)
            else { return }

        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.httpBody = "driverName=\(value)".data(using: .utf8)

        URLSession.shared.dataTask(with: request) { data, _, error in
            guard let data = data else { return }


            do {
                self.structure = try JSONDecoder().decode([AlreadyScheduledStructure].self,from:data)
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            }
            catch {
                print(error)
            }

            }.resume()

    }



    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return structure.count
    }

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

        let cell = tableView.dequeueReusableCell(withIdentifier: "scheduledID", for: indexPath) as! ScheduledCell
        let portfolio = structure[indexPath.row]
        cell.stopLabel.text = "Stop \(portfolio.stop_sequence)"
        return cell

    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


        let portfolio = structure[indexPath.row]
        let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "scheduledDelivery")

        print(portfolio.customer)
        let navTitle = portfolio.customer
        UserDefaults.standard.set(navTitle, forKey: "pressedScheduled")
        controller.navigationItem.title = navTitle
        navigationController?.pushViewController(controller, animated: true)
    }


    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 200.0
    }

}

请注意cellForRowAt我是如何将单元格设置为的dequeueReusableCell,这可能是应用程序有时在多次选择同一个单元格时崩溃的原因

let cell = tableView.dequeueReusableCell(withIdentifier: "scheduledID"

我还注意到,如果在 viewDidAppear 上重新加载 tableView 行,它不会经常崩溃,但当然,这是一个糟糕的解决方案。

我得到的错误:

'NSInternalInconsistencyException',原因:'试图为同一索引路径出列多个单元格,这是不允许的。如果您确实需要比表格视图请求更多的单元格出列,请使用 -dequeueReusableCellWithIdentifier: 方法

标签: iosswiftuitableviewuitableviewrowaction

解决方案


根据崩溃替换

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

let cell = tableView.dequeueReusableCell(withIdentifier: "scheduledID") as! ScheduledCell

推荐阅读