0

I'm following this tutorial because I want customized cells: https://www.youtube.com/watch?v=kYmZ-4l0Yy4

But it doesn't say how to add new cells so I just did this according to what I found here: How to insert new cell into UITableView in Swift

elements.append(["new data"])
tableView.beginUpdates()
tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: elements.count-1, inSection: 0)], withRowAnimation: .Automatic)
tableView.endUpdates()

I switched elements array from let to var because it gave me an error

And then the moment it gets to tableview.beginUpdates() or tableview.reloadData() it crashes giving me this error:

"Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value"

This is the rest of the code:

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellActiveCastingSites") as!TableViewCell_CastingSite

    cell.cellView.layer.cornerRadius = 15
    cell.lblCastingName.text = elements[indexPath.row].0
    cell.lblCastingWebsite.text = elements[indexPath.row].1

    cell.CastingLogo.image = UIImage(named: "CastingLogo_" + elements[indexPath.row].0)

    cell.CastingLogo.layer.cornerRadius = 20
    cell.CastingLogo.layer.masksToBounds = true

    cell.CastingLogo.layer.borderWidth = 1
    cell.CastingLogo.layer.borderColor = UIColor.white.cgColor

    return cell
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Curtain Call LLC
  • 144
  • 1
  • 1
  • 11
  • 1
    Can you show your `cellForRowAtIndexPath` – Quoc Nguyen Feb 05 '18 at 02:50
  • I added it now.. I'm new to stack overflow sorry about the incorrect formatting lol – Curtain Call LLC Feb 05 '18 at 03:08
  • Your line `tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: elements.count-1, inSection: 0)], withRowAnimation: .Automatic)` seems to be written in Swift 2. You need to update that line. – rmaddy Feb 05 '18 at 03:36
  • Have you connected your `tableView` outlet to your storyboard? – Paulw11 Feb 05 '18 at 04:26
  • Hey @rmaddy it crashes way before it even gets to the line.... – Curtain Call LLC Feb 05 '18 at 07:57
  • @Paulw11 I have this, is this what you mean? `@IBOutlet weak var tableView: UITableView!` – Curtain Call LLC Feb 05 '18 at 07:59
  • @QuocNguyen Hi I added it to the question... do you have any idea maybe? – Curtain Call LLC Feb 05 '18 at 08:00
  • You need to connect that outlet to the scene in the storyboard – Paulw11 Feb 05 '18 at 08:02
  • @CastingMonkeys: element of `elements` array has 0 and 1 (array with 2 element), but when you add data, you only add ["newdata"] (only 1 element). So it crash. I think so – Quoc Nguyen Feb 05 '18 at 08:09
  • @QuocNguyen no I tried that already, I just wanted to simplify that's why I wrote it like that, but actually even if I don't add ANY new data, just reload, it crashes :( – Curtain Call LLC Feb 05 '18 at 08:31
  • @Paulw11 What do you mean? in the Referencing Outlets it's connected - is there something else that I am missing? – Curtain Call LLC Feb 05 '18 at 08:35
  • Set a breakpoint and check; something is `nil` and the tableview outlet seems the most likely. – Paulw11 Feb 05 '18 at 08:36
  • @Paulw11 I set a breakpoint, it goes to this line `@IBOutlet weak var tableView: UITableView!` then comes back to the line `tableView.reloadData()` and then the moment I click f7 again, it crashes. It won't allow me to step into the reloadData() function so I don't know what's going on – Curtain Call LLC Feb 05 '18 at 08:41
  • That is because your `tableView` outlet is `nil`. When you hit the breakpoint type "po tableView" in the debugger console; it will show nil. You need to fix the connection in your storyboard. – Paulw11 Feb 05 '18 at 08:48
  • @Paulw11 Yes you're right - it is returning nil!! do I have to delete the current connection and do it again? – Curtain Call LLC Feb 05 '18 at 09:04
  • @Paulw11 so after deleting everything and starting from scratch - turns out the error was actually just the fact that I was calling the function of inserting a new element through another ViewController.... – Curtain Call LLC Feb 06 '18 at 12:57
  • @QuocNguyen so after deleting everything and starting from scratch - turns out the error was actually just the fact that I was calling the function of inserting a new element through another ViewController.... – Curtain Call LLC Feb 06 '18 at 12:58

0 Answers0