Reputation: 495
I'm new to Swift development and following many online tutorials. The majority of these Tutorials are referring to older versions on Xcode and the code is resulting in errors. Can anyone explain why the code below produces 'UITableViewCell?' is not convertible to UITableViewCella and how I go about fixing this problem.
I am using Xcode 7.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("customcell") as UITableViewCell
cell.textLabel?.text = array[indexPath.item]
return cell
}
Upvotes: 0
Views: 988
Reputation: 285069
There are two methods
1)
func dequeueReusableCellWithIdentifier(_ identifier: String) -> UITableViewCell?
which returns an optional so you have to unwrap the optional
let cell = tableView.dequeueReusableCellWithIdentifier("custom cell")!
The other method is preferable
2)
func dequeueReusableCellWithIdentifier(_ identifier: String,
forIndexPath indexPath: NSIndexPath) -> UITableViewCell
because it returns a non-optional
let cell = tableView.dequeueReusableCellWithIdentifier("custom cell", forIndexPath:indexPath)
Upvotes: 1
Reputation: 6150
The function returns UITableViewCell , while you are returning UITableViewCell? check the type of cell if its of type UITableViewCell? , you need to return cell!
( while cell is optional, the "!" unwrap it)
Upvotes: 0