-2

I am Getting

fatal error: Index out of range" and "Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)" at line "cell.textLabel?.text = String(arrayTable[indexPath.row])

When I test my function in playground it works as expected. My array get populated. So, I don't understand why Xcode won't find any value in my array.

any idea?

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var slider: UISlider!

@IBAction func sliderSelector(_ sender: Any) {
   arrayTable = [Int]()
    tableGenerate()
}

var arrayTable = [Int]()


public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 50
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style:UITableViewCellStyle.default, reuseIdentifier: "Cell")
    cell.textLabel?.text = String(arrayTable[indexPath.row])
    return cell
}

func tableGenerate () {
    var tableMultiplier = 1
    while tableMultiplier <= 50 {
        arrayTable.append(tableMultiplier * Int(slider.value))
        tableMultiplier += 1
    }

}
2
  • fatal error: Index out of range Means numberOfRowsInSection not sync with cellForRowAtIndexPath with your array count. Commented Aug 17, 2017 at 10:35
  • as suggested below, changing return 50 to return arrayTable.count fixed the crash Commented Aug 17, 2017 at 11:25

4 Answers 4

2

Pretty common mistake for newbies is to return from numberOfRowsInSection any magic number. Instead of that you should return your real items count: return arrayTable.count and i believe all should work fine.

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

8 Comments

but the after initialize array the tablview not update so u should to reload its data
it fixed the crash but the table does not display the array, looking for that now, @DivyeshGondaliya do you know how to reload ?
check my answer @Dev0urCode
I did but I'm sorry I do not understand where you mean to reload ?
@Dev0urCode just call tableView.reloadData().
|
1

Try to make changes like this

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

    func tableGenerate () {
        var tableMultiplier = 1
        while tableMultiplier <= 50 {
            arrayTable.append(tableMultiplier * Int(slider.value))
            tableMultiplier += 1
        }
      DispatchQueue.main.async {
         print(arrayTable)
         tblview.reloadData()
      }
    }

16 Comments

create your tableview object first and then reload it here its working and also check that your tableview datasource or delegate given or not @Dev0urCode
I write what you suggested but I get "Ambiguous reference to member 'tableView(_:numberOfRowsInSection:)'" I also did put the tableGenerate function after the tableView functions
youtube.com/watch?v=I4jSwWmqfOA watch this and get note where you not doing well @Dev0urCode
I can't see any tableView.reloadData () anywhere in the video :(
just watch you give delegate datasource method to your viewcontroller or not or how to create object of tableview @Dev0urCode
|
0

When the view loads, your tableView will attempt to return 50 cells. For each cell you're trying to assign the textlabel text to:

cell.textLabel?.text = String(arrayTable[indexPath.row])

But because the arrayTable is empty when the view loads, the application crashes when it's trying to access an index by indexPath.row.

Comments

0

problem in this method

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

Comments

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.