TableView为Nil [英] TableView is Nil

查看:138
本文介绍了TableView为Nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程:

import UIKit
import CloudKit

class FirstViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var listTableView: UITableView!
    var list: TLListModel!
    var specificList: CKRecord!




    override func viewDidLoad()
    {
        super.viewDidLoad()
        let myContainer = CKContainer.default()
        list = TLListModel(container: myContainer, viewController: self)
        if(listTableView != nil){
            listTableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        }

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print("number of items: %i", list.lists.count)
        return list.lists.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:UITableViewCell = listTableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell
        let list: CKRecord = self.list.lists[(indexPath as NSIndexPath).row]
        cell.textLabel?.text = list.value(forKey: "ListName") as? String
        cell.textLabel?.font = UIFont (name: "Slim Joe", size: 20)
        cell.accessoryType = .disclosureIndicator
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("This object has been selected")
        print(self.list.lists[(indexPath as NSIndexPath).row])

        specificList = self.list.lists[(indexPath as NSIndexPath).row]
        performSegue(withIdentifier: "TLSpecificListSegue", sender: nil)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "TLSpecificListSegue"{
            if let destinationVC = segue.destination as? TLSpecificListViewController{
                destinationVC.listObject = specificList
            }
        }
    }

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
    {
        return true
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
    {
        if editingStyle == .delete
        {
            let cloudkit = TLCloudKitHelper()            
            cloudkit.deleteListItem(self.list.lists[(indexPath as NSIndexPath).row], callback: { (listName) in
                TLAlertHelper.notifyUser("List Deleted", message: NSString(format: "List for %@ successfully deleted", listName) as String, sender: self)

                 let myContainer = CKContainer.default()
                 self.list = TLListModel(container: myContainer, viewController: self)
                DispatchQueue.main.async {
                    self.listTableView.reloadData()
                }

            })


        }
    }

}

当我使用以下方法从另一个视图控制器调用它时:

When I call it from another view controller using the following method:

@IBAction func createListAction(_ sender: AnyObject) {
    let cloudkit = TLCloudKitHelper()
    let listArray = createListFromTextField(textInputArea.text)

    if(!(listNameTextField.text?.isEmpty)!){
        cloudkit.createList(listNameTextField.text!) { (response) in
            let listId = response
            if (!listArray.isEmpty){
                for item in listArray{
                    cloudkit.saveItemRecord(item, listId: listId, recordName: response)
                }
            }
            let fvc: FirstViewController = FirstViewController()
            DispatchQueue.main.async {
                self.present(fvc, animated: true, completion: nil)
            }

        }
    }else{
        TLAlertHelper.notifyUser("Give the list a name", message: "You need to give you list a name...", sender:self)
    }


}

我收到一条错误消息,说fatal error: unexpectedly found nil while unwrapping an Optional value

I get an error saying fatal error: unexpectedly found nil while unwrapping an Optional value

我不明白为什么会出现此错误.我尝试在此处查看答案:在Swift中使用简单的UITableView-意外发现nil ,但我的回答都没有帮助.有人可以告诉我为什么这会崩溃吗,我该如何解决?

I don't understand why I am getting this error. I've tried looking at the answers here: Simple UITableView in Swift - unexpectedly found nil but I none of those answers helped. Can someone tell me why this this crashing and how I can fix it?

推荐答案

问题是此行:

let fvc: FirstViewController = FirstViewController()

这将创建一个空白 FirstViewController实例-一个与您在情节提要中设计的界面完全不连接的实例.它的视图是 empty .它没有表格视图.因此,由于没有表视图,因此任何表视图都没有插座连接,并且listTableView保留为nil.

This creates a blank FirstViewController instance — one completely unconnected with the interface you designed in the storyboard. Its view is empty. It has no table view in it. Therefore, since there is no table view, there is no outlet connection from any table view, and listTableView remains nil.

您要做的是从情节提要中获取FirstViewController实例 ,您已经在情节提要中设计了该对象的界面 .您可以通过与情节提要 进行对话,并使用FirstViewController的 identifier 来实现,即调用instantiateViewController(withIdentifier:). (为此,您可能必须在情节提要中给出 FirstViewController一个标识符.)

What you want to do is get the FirstViewController instance from the storyboard, the one whose interface you have already designed in the storyboard. You can do that by talking to the storyboard and using the FirstViewController's identifier, i.e., call instantiateViewController(withIdentifier:). (You might have to give the FirstViewController in the storyboard an identifier for this purpose.)

这篇关于TableView为Nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆