I ran into the following error in this code:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("Cell") as? UITableViewCell
ERROR: Downcast from 'UITableViewCell?' to 'UITableViewCell' only unwraps optionals; did you mean to use '!'?
Any Ideas?
In Swift2.0
method dequeueReusableCellWithIdentifier
is declare as:
@available(iOS 6.0, *)
func dequeueReusableCellWithIdentifier(identifier: String, forIndexPath indexPath: NSIndexPath) -> UITableViewCell
You shouldn't cast UITableViewCell
to UITableViewCell?
. See code below.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
// Configure the cell...
return cell
}
Hope this helps!
As of Xcode 7, dequeueReusableCellWithIdentifier
will always return a non optional UITableViewCell
.
You don't even need to specify the type, it can be written succinctly as:
let cell = tableView.dequeueReusableCellWithIdentifier("Cell")
or if you have a custom subclass of UITableViewCell
guard let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as? SomeOtherCell else { fatalError("unexpected cell dequeued from tableView") }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With