简体   繁体   中英

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.

For some reason, when I add the tableView.delegate = self , also 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. 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.

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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