0

I use Xcode7

i register the cell in viewDidLoad()

 let SLQHomeTableViewCellID = "SLQHomeTableViewCellID"
 self.tableView.registerClass(SLQStatusCell.self, forCellReuseIdentifier: SLQHomeTableViewCellID)

then the dataSouce like below:

// MARK: - Table view data source
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
  return statuses?.count ?? 0
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(SLQHomeTableViewCellID, forIndexPath: indexPath) as! SLQStatusCell
    let status = statuses![indexPath.row]
    cell.status = status
    return cell
}

i also override init()

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    // 初始化UI
    setupUI()

}

but when i run in simulator ,it's a fatal error in register cell ,this line is the error appeared

 self.tableView.registerClass(SLQStatusCell.self, forCellReuseIdentifier: SLQHomeTableViewCellID)

fatal error: unexpectedly found nil while unwrapping an Optional value

i want to why,i run the program in using OC is all right.

MrSong
  • 1
  • 1
  • Which line is causing the error? – rmaddy Oct 02 '15 at 04:26
  • self.tableView.registerClass(SLQStatusCell.self, forCellReuseIdentifier: SLQHomeTableViewCellID) – MrSong Oct 02 '15 at 04:33
  • Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – jtbandes Oct 02 '15 at 04:35
  • no, the error is appear on this line self.tableView.registerClass(SLQStatusCell.self, forCellReuseIdentifier: SLQHomeTableViewCellID) – MrSong Oct 02 '15 at 05:13

1 Answers1

0

If the line with the registerClass method call is causing that error, then chances are good the tableView IBOutlet is not connected. That is the most likely thing on that line of code that would be nil, as it probably has a type of UITableView! which is being implicitly unwrapped.

You can test this by setting a breakpoint and inspecting the value of self.tableView or with a simple print("Table view: \(self.tableView)") statement put above the existing registerClass line.

Charles A.
  • 10,685
  • 1
  • 42
  • 39
  • i am not using any xib or storyboard ,so using registerClass,and an print the class like below > + SLQHomeTableViewCellID fatal error: unexpectedly found nil while unwrapping an Optional value it cannot see any useful info – MrSong Oct 02 '15 at 04:48