Dev0urCode Dev0urCode -3 years ago 165
Swift Question

Xcode crashing when displaying array in table

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 =

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]()

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


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download