1

I'm using Tableview controller to make two prototype cells that app crash due to

thread 1 exc_bad_instruction (code=exc_i386_invop subcode=0x0) pointed to >>> return cell! in the first cell

fatal error: unexpectedly found nil while unwrapping an Optional value

   override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 10
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if (indexPath.section == 0) {
        let cell = tableView.dequeueReusableCellWithIdentifier("one", forIndexPath: indexPath) as? one
         cell?.textLabel?.text = "book"
        return cell!
    } else {

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

        cell.textLabel?.text = "not a book"
        return cell

    }

enter image description here

user3662992
  • 688
  • 1
  • 6
  • 16
  • Try putting an if let around table.dequene..., so that you can make sure that you actually have a prototype cell. And look back at the storyboard to make sure that you have set the identifier of the prototype cell. – Epic Defeater Jul 29 '15 at 00:58
  • You need to click on the prototype cell and set its identifier or maybe if that isn't the problem then the problem could be that you didn't set the class of cells so you need to go to the identity inspector of both of the cells and set its custom class. – Epic Defeater Jul 29 '15 at 01:18
  • yes I checked again both custom class . I changed the name it gave me Could not cast value of type 'UITableViewCell' (0x10ac14450) to – user3662992 Jul 29 '15 at 01:23

3 Answers3

2

If I may improve the code:

 let cellID = (indexPath.section == 0) ? "one" : "two"
 if let cell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath)
        as? UITableViewCell
     cell.textLabel!.text = (cellID == "one") ? "book" : "not a book"
     return cell
 } 
 else {
     assertionFailure("Unable to dequeue a cell of ID \(cellID)")
     return nil
 }

Then you'll get sensible error behavior. I'll bet that, even though you've named your classes one and two, that you have neglected to set their 'Identifier' within StoryBoard in the Attributes Inspector for each cell prototype.

Your answer is very wrong. You should not be creating lots of table cells. This is a misuse of tables. You should only use the ones on the reuse queue.

Use my version of the code, and set a breakpoint just after the if let cell. Analyze the object in the debugger and see what type it really is.

If instead you hit the assertionFailure, you still haven't really set the Identifier properly in StoryBoard. Show us a screen capture to convince us.

BaseZen
  • 8,650
  • 3
  • 35
  • 47
  • I got my answer from http://stackoverflow.com/questions/30774671/uitableview-with-more-than-one-custom-cells-with-swift – user3662992 Jul 29 '15 at 02:51
  • So out of curiosity, what did you actually miss? – BaseZen Jul 29 '15 at 02:56
  • I couldn't use your debugging code I just started learning swift I didn't know where to put it ! – user3662992 Jul 29 '15 at 02:59
  • It doesn't recognize imageview in tableview cell >> else if indexPath.row == 3 { let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell") as! TableViewCell cell.img.image = UIImage(named: "CJfSS2TW8AQ0CUu.jpg") return cell } – user3662992 Jul 29 '15 at 03:02
  • I didn't mean my code, I meant since you got your answer from that other question, I imagine you had most of the pieces in place, and the answer helped you find the final piece, so what was it? (BTW my code is just a complete replacement for your `cellForRowAtIndexPath` delegate method) – BaseZen Jul 29 '15 at 03:02
1

Looks like it's because the call

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

is doing a forced down cast of the cell to type two, and the object returned is not of type two. Using ! here is basically saying:

"Dequeue a cell (returns type AnyObject) and then downcast it two type two. I know down casts can fail if the type doesn't match, but in this case I'm sure it will. No need to handle errors"

As casts can fail, the cast returns an optional. In this case the cell returned can't be downcast as the types don't match. Check the code that registers the identifier "two", and check you are registering the right type of class (A UITableViewCell subclass named two)

Sam Clewlow
  • 4,293
  • 26
  • 36
0

I think you should edit code :

 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 2
    }
J.Arji
  • 631
  • 6
  • 18