7

Is the following implementation of cellForRowAtIndexPath the technically correct best-practice way taking into account the unwrapping of optionals

 class MyTableViewController: UITableViewController {
        var cell : UITableViewCell?
         // other methods here 
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        cell = tableView.dequeueReusableCellWithIdentifier("ItemCell")! as UITableViewCell
        let myItem = items[indexPath.row]

        cell!.textLabel?.text = myItem.name
        cell!.detailTextLabel?.text = myItem.addedByUser

        return cell!
      }

    }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Arunabh Das
  • 13,212
  • 21
  • 86
  • 109
  • 3
    There's no reason to have an optional cell variable outside the method, just make a non-optional local variable inside the method – dan Dec 17 '15 at 22:10
  • 1
    Tip: Create a new Xcode application with the "Master-Detail Application" template and you'll get sample code for free. – Martin R Dec 17 '15 at 22:12

1 Answers1

6

In Swift 2 dequeueReusableCellWithIdentifier is declared as

func dequeueReusableCellWithIdentifier(_ identifier: String,
                      forIndexPath indexPath: NSIndexPath) -> UITableViewCell

and cellForRowAtIndexPath is declared as

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

You see, No optionals!

The code can be reduced to

class MyTableViewController: UITableViewController {

   // other methods here 
   override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
     let cell = tableView.dequeueReusableCellWithIdentifier("ItemCell", forIndexPath: indexPath)
     let myItem = items[indexPath.row]

     cell.textLabel?.text = myItem.name
     cell.detailTextLabel?.text = myItem.addedByUser

     return cell
  }
}

In case of a custom table view cell the cell can be forced casted to the custom type.

let cell = tableView.dequeueReusableCellWithIdentifier("ItemCell", forIndexPath: indexPath) as! CustomCell

It's always a good idea to option-click on the symbol or use Quick Help to look up the exact signature.

vadian
  • 274,689
  • 30
  • 353
  • 361
  • If I try to do let cell = tableView.dequeueReusableCellWithIdentifier("ItemCell") as UITableViewCell without unwrapping, the compiler suggest that I should unwrap using !, as in let cell = tableView.dequeueReusableCellWithIdentifier("ItemCell")! as UITableViewCell Other than that, your code works!! – Arunabh Das Dec 17 '15 at 22:16
  • You don't need to unwrap anything because all relevant parameters are non optionals. – vadian Dec 17 '15 at 22:16
  • 1
    You said to use the second method that takes the index path parameter but your example is still using the wrong method – dan Dec 17 '15 at 22:17
  • Force unwrapping with "as!" is not a good suggestion by many lint tools. What's the better way? – Satyam Jun 19 '18 at 08:02
  • @Satyam Force unwrapping is not evil per se. There is more than just black and white. In this particular case force unwrapping is even recommended because it reveals a **design** error. If anything is hooked up correctly in Interface Builder the code must not crash. – vadian Jun 19 '18 at 08:12