0

Please help with the error I have on this line:

cell.name.text = names[indexPath.row] //error array  index out of range

This is my declaration:

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    var names = ["Anna","aanal"]
    var petnames = ["Anu","aalu"]

    @IBOutlet var tableView: UITableView!

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
        {

            let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
            cell.name.text = names[indexPath.row] //error array  index out of range
            cell.petname.text = petnames[indexPath.row]
            return cell
        }

        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }        
    }
8
  • Can you share the code on which array is being used to display the tableview? are you using names array or petnames array? Commented Jul 17, 2017 at 9:20
  • add numberOfRow method also in question . Commented Jul 17, 2017 at 9:20
  • 2
    You may access the value from the array list which is not available. For e.g.: If you have 4 cell in tableview and you have only 3 values in the array, in that situation it throws exception like index out of range Commented Jul 17, 2017 at 9:22
  • 2
    I see you have two different arrays; names and petNames; this is risky, since if the arrays get out of sync you can have errors such as the one you have experienced. It is better to have a single array of Structs, each with a name and a petName Commented Jul 17, 2017 at 9:22
  • 1
    Your array only has 2 elements while your numberOfRowsInSection method returns 3. Either change it to 2 or add 3 elements in your array Commented Jul 17, 2017 at 9:26

5 Answers 5

4

Your array only has 2 elements while your numberOfRowsInSection method returns 3. Either change it to 2 or add 3 elements in your array

The ideal way is to change

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

But usually dont work with 2 arrays and create a Entity object having name and petname as elements and then have an array of your entity class to populate tableview

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

Comments

0

Try this :

I think your are passed petnames in numberOfRowsInSection and names and petnames count are also different so this problem occurs .

Solution

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if names.count == petnames.count{
            return petnames.count

        }
        return 0;
    }

OR

You have passed more numberOfRowsInSection then array.count

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2 or names.count
    }

Comments

0

There are 2 values in your arrays names and petnames. However, there are 3 rows in your Table View. So, if the 3rd row of the table view is selected, it will be out of range of the array as there is no 3rd element in the arrays.

The solution is to make your names array and petnames array contain 3 elements:

var names = ["Anna","aanal", "boo"]
var petnames = ["Anu","aalu", "bo"]

or to return 2 rows in your table view:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 2
}

The best way is to return the count of the arrays (if the count of both arrays are different, return nothing):

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if names.count == petnames.count {
        return names.count
    }
    return 0
}

Comments

0

Just pass the count of array

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

1 Comment

If you are going to copy my comment to make an answer, at least go to the trouble of providing a complete answer including the definition of the struct.
0
var names = ["Anna","aanal"]  // you have 2 elemnts
var petnames = ["Anu","aalu"]

@IBOutlet var tableView: UITableView!

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //return 3 // you have returned 3 but it should be 2

    // it should be
    return 2
    //or 
    //  return names.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
    cell.name.text = names[indexPath.row] //you got error because you are trying to get 3 element from array where as there are only 2 elements 
    cell.petname.text = petnames[indexPath.row]
    return cell
}

It is better if you use your arrayname.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.