1

I'd like to set a value in the datailTextLabel of a UITableViewCell. If the code runs, the app crashes.

This is the error: fatal error: unexpectedly found nil while unwrapping an Optional value

This is the method in which the error takes place:

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

    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell

    cell.detailTextLabel!.text = "Date"      //error occurs

    return cell
}

Possible useful informations: https://www.dropbox.com/s/240sy3k1ic2hu0q/Bildschirmfoto%202015-02-28%20um%2021.03.35.png?dl=0

pablo
  • 61
  • 1
  • 1
  • 6

1 Answers1

0

Use:

let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

Also, there is no need to force unwrap it. Even if detailTextLabel exists, I would use cell.detailTextLabel?.text = "Date" to be safe and make sure your cell is a style that supports detailTextLabel. Ie. Detail Left, Detail Right, Subtitle.

Ian
  • 12,538
  • 5
  • 43
  • 62
  • I'm not using storyboard. How can I change the style programmatically? – pablo Feb 28 '15 at 20:09
  • `UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell")` Although there is more setup for creating a cell programmatically, but there are resources online for finding that – Ian Feb 28 '15 at 20:10