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: