iOS - 解包可选值时意外发现 nil

标签 ios uitableview

请帮忙,在我决定创建 loadData 函数并将其放在 if 子句中之前,代码就可以工作了。所有其他意外发现的 nil 修复都涉及更改单元格的声明内容,我已经尝试过了,我觉得这个问题是由我放置 loadData 的位置引起的。

谢谢。

import UIKit

class GroupsTableViewController: UITableViewController /* UITableViewDataSource, UITableViewDelegate */ {


let APP_ID = "IGNORE"
let SECRET_KEY = "IGNORE"
let VERSION_NUM = "v1"

var backendless = Backendless.sharedInstance()


@IBOutlet weak var table: UITableView!


var items=["Dog","Cat","Cow","Platypus"]

override func viewDidLoad() {
    super.viewDidLoad()


    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    backendless.initApp(APP_ID, secret:SECRET_KEY, version:VERSION_NUM)

    self.backendless.userService.getPersistentUser()
    let user = self.backendless.userService.currentUser

    if (user != nil) {
        self.loadData()
    } else {
        Utilities.loginUser(self)
    }

    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 120.0;


}

func loadData() {

    self.table.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    self.table.dataSource = self
    self.table.delegate = self

}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return items.count
}



override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

fatal error :在解包可选值时意外发现 nil:

    let cell = self.table.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell

    cell.textLabel!.text = self.items[indexPath.row]

    // Configure the cell...

    return cell
}

override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    print("You tapped on cell # \(indexPath.row)")
}

}

请帮忙,在我决定创建 loadData 函数并将其放在 if 子句中之前,代码就可以工作了。所有其他意外发现的 nil 修复都涉及更改单元格的声明内容,我已经尝试过了,我觉得这个问题是由我放置 loadData 的位置引起的。

谢谢。

最佳答案

我重写了两行以使您的代码更稳定并解决错误:

let cell = self.table.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel?.text = self.items[indexPath.row]

关于iOS - 解包可选值时意外发现 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35929751/

相关文章:

ios - 为我的容器 View 创建委托(delegate)

ios - 提取数据的 Realm 查询

ios - 打开 TableView 并选择行

ios - 如何在 swift ios 中的两个自定义表格 View 单元格之间添加间距?

ios - 如何从另一个 UITab 中的 UITableViewCell 连接选项卡中的 Map ViewController?

ios - AFNetworking 可达性 iOS

ios - 在基于 Objective-C 的 SDK 之间共享代码

ios - Swift 中的自调整单元格大小,如何以编程方式进行约束?

ios - dequeueReusableCellWithIdentifier :forIndexPath: 中的断言失败

ios - 展开 UITableViewCell UIImage