2

In the code below I initialized the Images array with a list of image file names. I want the order of the images to be aligned with the order of the Names array. This is what I attempted and I got an error saying

Thread 1: EXC_BAD_INSTRUCTION ( code=EXC_I386_INVOP, subcode = 0x0)

The console output: fatal error: Array index out of range (lldb)

Code

 class NonameTableViewController: UITableViewController {


    var Names = [" Ferro", "Korean", "HUH?","CatCafe", "UINITY", "FAKESTORE" ,"IRANOUTOFNAMES", "OKAY", "KEEP CODING"]

    var Images = ["cafedeadend.jpg", "homei.jpg", "teakha.jpg", "cafelois1.jpg"," petiteoyster.jpg", "forkeerestaurant.jpg"]

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


        let cellIndentifier = "Cell"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIndentifier, forIndexPath: indexPath)


        //configure cell 

         cell.textLabel?.text = Names[indexPath.row]
        cell.imageView?.image = UIImage(named: Images[indexPath.row])

            return cell
} 


       override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            // #warning Incomplete implementation, return the number of sections
            return 1
        }

        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            // #warning Incomplete implementation, return the number of rows
            return Names.count
        }
2
  • As the Names array has a higher count than Images I guess you are using the former to define the numberOfRowsInSection when trying to pick out an element from Images at an index outside the array's range it will crash like you describe... If this is not the case could you please show us your numberOfRowsInSection method? Commented Dec 17, 2015 at 5:26
  • I updated my question with your requested info thanks! @nickfalk Commented Dec 17, 2015 at 5:31

5 Answers 5

2

Just change your numberOfRowsInSection function:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return min(Names.count, Images.count)
}

This will assure that you only display image / name for the images that you have both a name and an image for.

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

Comments

0

"Array index out of range" means you're looking for the nth item in an array that has fewer than n items in it. Either:

  • your tableView:numberOfRowsInSection: method is returning a number larger than either your number of names or your number of images, or
  • said method is returning the number of names, which is greater than the number of images.

You should have as many images as you have names, or you should have a way of filling a large range of numbers by repeating selections from a smaller range. (Look into the modulus % operator.) And either way, you should tell the table view that you only have as many rows as you have data for them.

Comments

0

The number of rows from your numberOfRowsInSection is

Names.count // which is 9

But your Images array contains 6 image names. So you are getting that exception . Keep 6 names in your name array or 9 images in images array .

Comments

0

As others have said here, the crash/error is caused when you try to access an index that is out of the range of that array. Also, as pointed out, you can update the images array so it has the same number of elements as the names' array, or you can avoid accessing elements out of range by applying the solution described by nickfalk.

Another solution is to provide a default value for the case where the index is out of range:

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

    let cellIndentifier = "Cell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIndentifier, forIndexPath: indexPath)

    //configure cell 
    cell.textLabel?.text = names[indexPath.row]

    if let imageName = (indexPath.row >= images.count ? "placeholderImage.jpg" : images[indexPath.row]) as String? {
    cell.imageView?.image = UIImage(named: imageName)
    }

        return cell
}

This allows you to display all the names as also provide a fallback image when the image set in the array doesn't exist in your assets or the image name is misspelled.

Note that I changed the name of your var's to start with lower case letter, and I strongly suggest you to do same, following the naming conventions for Swift. See raywenderlich.com Swift Style Guide as an example.

I hope this helps with your problem.

Comments

0

There is mistake in numberOfRowsInSection and it will work definitely

return min(Names.count, Images.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.