ios - 按下 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: 方法
解决方案
根据崩溃替换
let cell = tableView.dequeueReusableCell(withIdentifier: "scheduledID", for: indexPath) as! ScheduledCell
和
let cell = tableView.dequeueReusableCell(withIdentifier: "scheduledID") as! ScheduledCell
推荐阅读
- javascript - 显示水平滚动条,因为绝对定位的元素
- flutter - 如何将 Flutter Audio Query 中的 SongInfo 列表添加到 Flutter 中的音频服务 MediaItem?
- android - 是否可以为 DataTables 制作动态高度?
- r - 字符串按另一列的值拆分
- google-app-engine - Google App Engine:请求在等待太久后中止
- tensorflow-federated - 联邦学习中的什么状态 = iterative_process.initialize()
- python - 这个语句在 Python 中有效吗?
- javascript - React Context API - Global Context Provider,这是否适用于全局上下文提供程序以避免嵌套?
- c# - 在属性中设置一个值 - Setter 从未到达
- sql - 如果当前表中不存在值,则在另一个表中搜索一个值