按下UITableView行时是否正确触发视图控制器打开? [英] Issue properly triggering a view controller to open when a UITableView row is pressed?

查看:119
本文介绍了按下UITableView行时是否正确触发视图控制器打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I properly trigger a view controller to open when a UITableView row is pressed?

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

I need to allow the user to go back from the view controller back to the tableView and allow them to select the same or a different tableView row.

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

The problem I am currently having is the application crashes when selecting the same row more than once after returning back from ViewController that opens when selecting on one of the rows: scheduledDelivery

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

Currently, this is the code I have:

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,这可能就是为什么当多次选择同一单元格时,应用有时崩溃的原因

Notice how in cellForRowAt I am setting the cell as a dequeueReusableCell which might be why the app is crashing sometimes when selecting the same cell more than once

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

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

I have also noticed that if the tableView rows are reloaded on viewDidAppear it does not crash as often, but of course, this is a terrible solution.

我得到的错误:

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

'NSInternalInconsistencyException', reason: 'Attempted to dequeue multiple cells for the same index path, which is not allowed. If you really need to dequeue more cells than the table view is requesting, use the -dequeueReusableCellWithIdentifier: method

推荐答案

根据崩溃替换

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

使用

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

这篇关于按下UITableView行时是否正确触发视图控制器打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆