gpt4 book ai didi

ios - 当按下 UITableView 行时正确触发 View Controller 打开的问题?

转载 作者:行者123 更新时间:2023-11-29 05:39:00 24 4
gpt4 key购买 nike

当按下 UITableView 行时,如何正确触发 View Controller 打开?

我需要允许用户从 View Controller 返回到 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', 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

关于ios - 当按下 UITableView 行时正确触发 View Controller 打开的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56756467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com