【问题标题】:TableView.delegate unexpectedly found nil - on a child view controllerTableView.delegate 意外发现 nil - 在子视图控制器上
【发布时间】:2021-12-12 07:13:06
【问题描述】:

我有一个 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()
}


【问题讨论】:

  • 您指定了框架吗?
  • 看起来你的 table 没有连接到 Interface Builder
  • 感谢您这么快回复。当我在情节提要中添加 tableview 时,框架设置了约束。还是我也需要以编程方式进行?
  • 如何创建WalletViewController 的实例以显示在侧边栏中?
  • 保罗,我使用 addChild()。如果命名 == " " 则 isHidden = false 或 true 取决于我试图呈现的视图

标签: ios swift xcode uitableview


【解决方案1】:

请更改注册 CELL Tableview 上的代码。

yourTableView.register(
   UINib.init(nibName: "ProductCVC", bundle: nil), 
    forCellWithReuseIdentifier: "ProductCVC")

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
  • 我打算用什么来更改 ProductCVC?不知道你从哪里得到的
猜你喜欢
  • 1970-01-01
  • 2020-03-28
  • 2020-10-14
  • 1970-01-01
  • 1970-01-01
  • 2014-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多