0

I have a UITableView that crashes when I start to scroll on it. The UITableView is a list of articles, each cell has an associated Headline and Image being pulled from a news API.

I have a placeholder image & an image if there is no image from the API, in my project assets.

WebListViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    WebListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WebListCell"];
    Feed *feedLocal = [headlinesArray objectAtIndex:indexPath.row];

    Images *imageLocal = [feedLocal.images objectAtIndex:0];
    NSString *imageURL = [NSString stringWithFormat:@"%@", imageLocal.url];
    NSLog(@"img url: %@", imageURL);

    __weak UITableViewCell *wcell = cell;
        [cell.imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", imageURL]]
                       placeholderImage:[UIImage imageNamed:@"background.png"]
                       completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                         if(image == nil) {
                         //realign your table view cell
                           [wcell.imageView setImage:[UIImage imageNamed:@"placeholder.png"]];
                                       //];
                                  }
                              }];
    return cell;
}

The UITableView crashes when I start to scroll down the list if an article does not have an Image coming back from the API, even I want it to just use the image from my assets in those cases.

The error is * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

Thanks for the help! Will post any code as needed!

EDIT:

Images *imageLocal = [feedLocal.images objectAtIndex:0];

... looks like the line its crashing on

Also, here is what the JSON response looks like for the empty image array in their API explorer for testing: enter image description here

Realinstomp
  • 532
  • 2
  • 13
  • 30
  • Try to place exception breakpoint and see on which particular line the error appears. – Valent Richie Jul 03 '13 at 23:42
  • Are you sure it has to do with the image and not your data array and it's index. Are you updating the data from the api when scrolling? – Yan Jul 03 '13 at 23:53
  • I edited the code to show the line it looks like its getting stuck on. `Images *imageLocal = [feedLocal.images objectAtIndex:0];` line with the error "Thread 1: signal SIGBART". Any ideas? Thanks so much for the help @verbumdei – Realinstomp Jul 03 '13 at 23:58
  • @Yan Thanks for the response! I added some code and additional info to the question, and what your saying makes sense, that it might be the data array. Does the info I added help clear up your question, and if so, what solution would you suggest? – Realinstomp Jul 04 '13 at 00:01
  • It only crashes when you scroll? actually i think you meant to do Images *imageLocal = [feedLocal.images objectAtIndex:indexPath.row]; Also you .images array becomes blank. Are you adding items to it after you start scrolling? It would be easier if you can post the code where you are creating feedLocal.images array – Yan Jul 04 '13 at 00:06
  • I added code above per your request to show how I'm getting feedLocal, let me know if you need more. As far as why its crashing when I scroll, I think its because the images that have an empty array are still offscreen, so as I scroll, once I get to a cell with an empty array thats when the cell starts to load but crashes due to the empty array its trying to pull the image from. Does that make sense? – Realinstomp Jul 04 '13 at 00:12

1 Answers1

2

Based on the error message, you can deduce that the feedLocal.images array is actually empty and you try to get the first object in the array when the error happens.

You might want to do additional checking first before getting the first object of the array:

if (feedLocal.images.count == 0) {
// do what you need to do if the array is empty, for example skip the loading of the imageView
}

For example:

if (feedLocal.images.count == 0) {
    [cell.imageView setImage:[UIImage imageNamed:@"placeholder.png"]];
}
else {
    Images *imageLocal = [feedLocal.images objectAtIndex:0];
    NSString *imageURL = [NSString stringWithFormat:@"%@", imageLocal.url];
    NSLog(@"img url: %@", imageURL);

    __weak UITableViewCell *wcell = cell;
    [cell.imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", imageURL]]
                   placeholderImage:[UIImage imageNamed:@"background.png"]
                          completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                         if(image == nil) {
                         //realign your table view cell
                           [wcell.imageView setImage:[UIImage imageNamed:@"placeholder.png"]];
                                       //];
                                  }
                              }];
}
Valent Richie
  • 5,226
  • 1
  • 20
  • 21
  • Thanks! Where would I put this if statement in the process of the block of code I showed? – Realinstomp Jul 04 '13 at 00:13
  • @Reez updated my answer with an example. But it is only my guess of what you are trying to accomplish. – Valent Richie Jul 04 '13 at 00:17
  • I think what your trying to do is definitely what I'm trying to do, so thanks. I'm getting an error because of the `length` part saying "Property 'length' not found on object of type NSArray"... what should I do for that? – Realinstomp Jul 04 '13 at 00:20
  • @Reez sorry, I remembered the wrong property. Updated my answer. It should be count. – Valent Richie Jul 04 '13 at 00:26