[英]Swift: I got “ Unexpectedly found nil ” in “tableView.delegate=self”
[英]TableView.delegate unexpectedly found nil - on a child view controller
我有一个 tableView,它是通过 sideMenu 访问的,其中这个特定的 viewController 是一个子 viewController。
出于某种原因,当我添加tableView.delegate = self
,还有tableView.dataSource
并注册单元格。 我的应用程序崩溃了。 当我将此视图控制器设置为初始视图控制器时,表视图会显示出来,但是当我将其返回到我的主页时,应用程序崩溃,并且不想加载。
我的 IBOutlets 连接正确。 我什至尝试在故事板中连接数据源和委托。
我添加了两种类型的代码供大家查看,第一种是 IBOutlet,第二种是我尝试了 addSubView(table) 但是,没有表出现。
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(table)
table.delegate = self
table.dataSource = self
table.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
这是我的钱包视图控制器
//
// WalletViewController.swift
// handlwithcare
//
// Created by Charles Morgan on 16/10/2021.
//
import UIKit
import RealmSwift
class WalletViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let button = UIButton()
@IBOutlet var table: UITableView!
private var data = [AddCardItem]()
override func viewDidLoad() {
super.viewDidLoad()
table.delegate = self
table.dataSource = self
table.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
view.backgroundColor = UIColor(named: "RED")
button.setTitle("Add New Card", for: .normal)
view.addSubview(button)
button.backgroundColor = UIColor(named: "yellow-2")
button.setTitleColor(UIColor(named: "RED"), for: .normal)
button.frame = CGRect(x: 50, y: 800, width: 350, height: 50)
button.layer.cornerRadius = 15
button.addTarget(self, action: #selector(didTapAddButton), for: .touchUpInside)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = data[indexPath.row].item
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
@objc private func didTapAddButton() {
let rootVC = addCardViewController()
let navVC = UINavigationController(rootViewController: rootVC)
navVC.modalPresentationStyle = .fullScreen
present(navVC, animated: true)
}
}
class AddCardItem: Object {
@objc dynamic var item: String = ""
@objc dynamic var date: Date = Date()
}
这是我第二次尝试使用view.addSubView
import UIKit
import RealmSwift
class WalletViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let button = UIButton()
let table = UITableView()
private var data = [AddCardItem]()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(table)
table.delegate = self
table.dataSource = self
table.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
view.backgroundColor = UIColor(named: "RED")
button.setTitle("Add New Card", for: .normal)
view.addSubview(button)
button.backgroundColor = UIColor(named: "yellow-2")
button.setTitleColor(UIColor(named: "RED"), for: .normal)
button.frame = CGRect(x: 50, y: 800, width: 350, height: 50)
button.layer.cornerRadius = 15
button.addTarget(self, action: #selector(didTapAddButton), for: .touchUpInside)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = data[indexPath.row].item
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
@objc private func didTapAddButton() {
let rootVC = addCardViewController()
let navVC = UINavigationController(rootViewController: rootVC)
navVC.modalPresentationStyle = .fullScreen
present(navVC, animated: true)
}
}
class AddCardItem: Object {
@objc dynamic var item: String = ""
@objc dynamic var date: Date = Date()
}
请更改注册 CELL Tableview 上的代码。
yourTableView .register(UINib.init(nibName: "ProductCVC", bundle: nil), forCellWithReuseIdentifier: "ProductCVC")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.