简体   繁体   English

TableView.delegate 意外发现 nil - 在子视图控制器上

[英]TableView.delegate unexpectedly found nil - on a child view controller

I have a tableView, that is accessed via a sideMenu where this particular viewController is a child viewController.我有一个 tableView,它是通过 sideMenu 访问的,其中这个特定的 viewController 是一个子 viewController。

For some reason, when I add the tableView.delegate = self , also tableView.dataSource出于某种原因,当我添加tableView.delegate = self ,还有tableView.dataSource

and register the cell.并注册单元格。 My app crashes.我的应用程序崩溃了。 When I set this view Controller as the initial view controller the table view shows up, however when I return it to my home page, the app crashes, and does not want to load.当我将此视图控制器设置为初始视图控制器时,表视图会显示出来,但是当我将其返回到我的主页时,应用程序崩溃,并且不想加载。

My IBOutlets are connected correctly.我的 IBOutlets 连接正确。 I even tried connecting the data source and delegate in storyboard.我什至尝试在故事板中连接数据源和委托。

I have added two types of code for you all to look at, the first, has an IBOutlet, the second I have tried an addSubView(table) however, no table appears.我添加了两种类型的代码供大家查看,第一种是 IBOutlet,第二种是我尝试了 addSubView(table) 但是,没有表出现。

override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(table)
        table.delegate = self
        table.dataSource = self
        table.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

Here is my Wallet View Controller这是我的钱包视图控制器


//
//  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()
}


Here is my Second attempt with a view.addSubView这是我第二次尝试使用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()
}


Please change the code on the register CELL Tableview.请更改注册 CELL Tableview 上的代码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM