0

I have some data which is pulled from a DB and stored in an array within my UITableViewController. However, when I try to put the data inside each of my cells, I get an error that my index is out of the bounds of the array.

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return cellItems.count
}

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let item: ItemPreBasketModel = cellItems[indexPath.row] as! ItemPreBasketModel
    if indexPath.row == 0 {
        //Desc cell
        let cellIdentifier: String = "descCell"
        let descCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
        return descCell
    } else if indexPath.row == 1 {
        //Price cell
        let cellIdentifier: String = "priceCell"
        let priceCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
        return priceCell
    } else if indexPath.row == 2 {
        //Quantity cell
        let cellIdentifier: String = "quantityCell"
        let quantityCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
        return quantityCell
    } else {
        //Submit cell
        let cellIdentifier: String = "submitCell"
        let submitCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
        return submitCell
    }
}

Here is the cellItems variable and also where it is populated:

var cellItems: NSArray = NSArray() {
    didSet {
        tableView.reloadData()
    }
}

    func itemsDownloaded(items: NSArray) {
    cellItems = items
    tableView.reloadData()
}

Here is the static table view in the storyboard:

setup of the table view

What I want to do, which I've took out of the code for as it still fails before reaching this part, is to use the 'item' variable I declare inside cellForRowAt and populate each of my cell outlets with a part of the object,

i.e: descCell.descLabel.text = item.name

The error I get is:

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

on the line

let descCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
1
  • wr u r getting error ? Commented Feb 7, 2018 at 12:50

1 Answer 1

1

You should not use a table view controller with static cells and cellForRowAt at the same time. You should use dynamic prototypes to populate an array dynamically.

To change the content type of your table view, select it in Interface Bulder, open the Attributes inspector (the fourth icon from the left in the right sidebar) and select Dyanmic Prototypes for Content under the Table View section.

Dynamic Protoypes setting in Attributes inspector

Sign up to request clarification or add additional context in comments.

3 Comments

Ah right, should I just load each cell from a xib file and completely bypass static cells ?
@JamLis You can define prototype cells in Interface Builder as well, you don't necessarily need to use separate xib files.
Got you, thanks for the quick reply. I will mark your answer, change to prototype and repost a new thread if any problems arise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.