Maha
Maha

Reputation: 529

fatal error: unexpectedly found nil while unwrapping an Optional value swift When Selecting tableView Cells

My tableView displays list of cards

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

    let cell = cardsTable.dequeueReusableCellWithIdentifier("SelectableCardCell", forIndexPath: indexPath) as! CardSelectionTableViewCell
    let cardCell: Card!
    if self.delegate is CardsApprovalsViewController {
        cardCell = approvalCards[indexPath.row] // approvalCard array of type card
    } else {
        cardCell = fetchedResultsController.objectAtIndexPath(indexPath) as! Card
    }
    cell.policyHolderName.text = cardCell.policy_holder_name
    cell.alphaMark.text = cardCell.policy_holder_name.substringToIndex(advance(cardCell.policy_holder_name.startIndex, 1)).uppercaseString
    var image: NSData = cardCell.photo as NSData
    cell.picture.highlightedImage = UIImage(data: image)
    cell.cardNumber.text = cardCell.member_id
    cell.policyNumberLabel.text = cardCell.policy_no
    cell.insuranceNameLabel.text = cardCell.insurance.company_name
    return cell
}

Afetr selecting one cell, i want cell.alphaMark label to be hidden in order to display cell.picture.highlightedImage

and if the user select another cell, cell.alphaMark for the previous cell should appear again

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var selectedCell: CardSelectionTableViewCell = cardsTable.cellForRowAtIndexPath(indexPath) as! CardSelectionTableViewCell
    selectedCell.alphaMark.hidden = true
    let newSwiftColor = UIColor(red: 224, green: 227, blue: 224)
    selectedCell.contentView.backgroundColor = newSwiftColor
    if self.delegate is CardsApprovalsViewController {
        self.card = approvalCards[indexPath.row]
    } else {
        self.card = fetchedResultsController.objectAtIndexPath(indexPath) as! Card
    }
}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    var selectedCell = cardsTable.cellForRowAtIndexPath(indexPath) as! CardSelectionTableViewCell // Some times crashes here 
    selectedCell.alphaMark.hidden = false
}

When i select cell and the select another cell ...etc (Selecting cells repeatedly) alphaMark and picture.highlightedImage works fine, but then it stops and gives me "unexpectedly found nil while unwrapping an Optional value" error

I checked the outlets of the cell and they are connected to the storyboard

Can someone help me with this error ? Thanks

Upvotes: 0

Views: 690

Answers (1)

Andy Obusek
Andy Obusek

Reputation: 12832

The easiest thing to do to prevent the crashing, is to change from forced unwrapping of the optional and use an if let instead:

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    if let selectedCell = cardsTable.cellForRowAtIndexPath(indexPath) as? CardSelectionTableViewCell {
        selectedCell.alphaMark.hidden = false
    }
}

This will take into account that cardsTable.cellForRowAtIndexPath(indexPath) may return nil, so trying to unwrap it into CardSelectionTableViewCell won't fail (because you can't unwrap nil into a CardSelectionTableViewCell)

That will prevent the crashing, but it won't entirely accomplish what you are trying to do. You'll also need to update cellForRowAtIndexPath to check if the current cell is selected and update alphaMark.hidden appropriately.

So one quick crack at how to do that would be to add this to the end of cellForRowAtIndexPath so that each time the cell is brought on screen, it's alphaMark is conditionally hidden based on the cell being selected:

cell.alphaMark.hidden = cell.selected

Upvotes: 3

Related Questions