gpt4 book ai didi

ios - TableView 为零

转载 作者:行者123 更新时间:2023-11-28 19:34:44 26 4
gpt4 key购买 nike

我有以下类(class):

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

})


}
}

}

当我使用以下方法从另一个 View Controller 调用它时:

@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: unwrappedly found nil while unwrapping an Optional value

我不明白为什么会出现此错误。我试过在这里查看答案:Simple UITableView in Swift - unexpectedly found nil但我这些答案都没有帮助。有人可以告诉我为什么会崩溃以及如何修复它吗?

最佳答案

问题是这一行:

let fvc: FirstViewController = FirstViewController()

这会创建一个空白 FirstViewController 实例——一个与您在 Storyboard中设计的界面完全无关的实例。它的 View 是空的。它里面没有表格 View 。因此,由于没有 TableView ,因此没有来自任何 TableView 的导出连接,并且 listTableView 保持为 nil

您要做的是从 Storyboard获取 FirstViewController 实例,您已经在 Storyboard中设计了它的界面。您可以通过 Storyboard并使用 FirstViewController 的标识符来做到这一点,即调用instantiateViewController(withIdentifier:)。 (为此目的,您可能必须 Storyboard中的 FirstViewController 一个标识符。)

编辑 这是一个很常见的错误,我写了一篇关于它的博文:http://www.programmingios.net/dont-make-a-new-instance-by-mistake/

关于ios - TableView 为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40077498/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com