0

i can't figure out why my array is out of bounds. i put the feed items into an array "imageArray" and it gives me fatal error heres code

   override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! BlogPost

    let blogPost: BlogPost = blogPosts[indexPath.row]
    cell.textLabel?.text = blogPost.postTitle
   // cell.textLabel?.text = blogPost.postImageLink
    if cell.postImage?.image == nil {
  //  let cache = ImageLoadingWithCache()
      //  cache.getImage(self.postImageLink,  imageView: cell.postImage, defaultImage: "IMG_0079")}
   if cell.postImage?.image == nil {

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {

        // retrieve image from url
        let image = UIImage(data: NSData(contentsOfURL: NSURL(string:self.postImageLink)!)!)
        self.imageArray.append(image!)
        dispatch_async(dispatch_get_main_queue(), { Void in
            // set image in main thread

            cell.postImage?.image =  self.imageArray[indexPath.row]
        })
    }
    } else {
            cell.postImage?.image = UIImage(named: "IMG_0079")
        }

    }

    return cell
}

specifically the error is here

 cell.postImage?.image =  self.imageArray[indexPath.row]

any ideas?

Dylan Godfrey
  • 103
  • 2
  • 10
  • try using this extension UIImageView http://stackoverflow.com/questions/24231680/loading-downloading-image-from-url-on-swift/27712427#27712427 – Leo Dabus Jul 14 '16 at 00:34
  • I'm going to guess it's because `indexPath` is captured when the closure is created and then by the time the closure is called the `indexPath.row` is stale and out of bounds. Not to mention that you are force-unwrapping a lot in there, what if some of those `Optional` are unset? Test and unwrap them properly! –  Jul 14 '16 at 00:40

1 Answers1

1

Your TableView number of rows in section must have the same value as your imageArray count. That signify : if you have 10 cells and only 9 images, the 10th cell would look for the 10th image which do not exist.

To be sure that the error does not appear anymore, just add the following if statement before setting your cell :

if (indexPath.row < self.imageArray.count) { }

Or as ColGraff said

A guard statement would better indicate what you're doing

guard indexPath.row < self.imageArray.count else { return } 

Before your cell edition.

Those two solutions should avoid you any troubles.

Community
  • 1
  • 1
  • A `guard` statement would better indicate what you're doing: `guard indexPath.row < self.imageArray.count else { return }` –  Jul 14 '16 at 01:37
  • Yes, for sure, but both work ! It is up to him to decide if he prefer use it or not ! A lot of different solutions work ^^ – Damien Ballenghien Jul 14 '16 at 01:39
  • Of course! I added the comment because this is a good place to use `guard`, this situation is one reason it was added to the language. –  Jul 14 '16 at 01:42
  • i appreciate the help i used the guard statement before ""cell.postImage?.image = self.imageArray[indexPath.row]" is that correct? Also this did get rid of my problem I'm just double checking – Dylan Godfrey Jul 14 '16 at 15:58