Reputation: 59
Please help, the code worked before I decided to create the loadData function and place it inside an if clause. All the other unexpectedly found nil fixes involve changing what the cell is declared as, I've tried that, and I feel like this problem is caused by where I placed loadData.
Thank you.
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: unexpectedly found nil while unwrapping an Optional value:
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)")
}
}
Please help, the code worked before I decided to create the loadData function and place it inside an if clause. All the other unexpectedly found nil fixes involve changing what the cell is declared as, I've tried that, and I feel like this problem is caused by where I placed loadData.
Thank you.
Upvotes: 2
Views: 208
Reputation: 6369
I rewrite two lines to make your codes more stable and solve the bug:
let cell = self.table.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel?.text = self.items[indexPath.row]
Upvotes: 2