0

I'm using RestKit to pull data from a web service API, and when the API response with an empty array for "Image", the UITableView crashes.

I was wondering if an "if statement" would be best to solve the problem?

cellForRowAtIndexPath, which works perfectly when there is an Image Array:

Images *imageLocal = [apiResponseLocal.images objectAtIndex:0];
NSString *imageURL = [NSString stringWithFormat:@"%@", imageLocal.url];

My best guess that doesn't work for the "if statement"

if ([imageURL isEqualToString:@""])
    {
    [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://staticimage.com/to/use/when/no/image/from/api"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    }
    else
    {
    [cell.imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", imageURL]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    }

I'm not sure if I'm on the right trail trying to solve with an "if statement", or what that actual statement might be. Any help would be much appreciated!

EDIT: Error & Crash I get when there is no image in the NSArray and I try scrolling the TableView toward that particular cell is "_NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array"

5
  • Well obviously you're not going to get much of an image from an empty string, if that's what you're asking. Commented May 25, 2013 at 20:02
  • @HotLicks correct. I'm trying to place an Image in the ImageView spot if the Image array returned from the API is empty. You know what I mean? If I don't get anything back from them, I can put a Image of my own in there. Commented May 25, 2013 at 20:08
  • So what is your question? Commented May 25, 2013 at 20:49
  • The TableView crashes as it stands now saying: "_NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array". So I need to create some way for if there is no image in the array, for the TableView to not crash because of that specific TableViewCell. Know what I mean? Commented May 25, 2013 at 20:52
  • So what line does the crashing noise come from? Commented May 26, 2013 at 0:21

1 Answer 1

3

You need to check your array count and handle if it is less than 1 before accessing any index. That's the place to do your default no-image image URL logic.

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

1 Comment

Thanks for the helpful response! I will try that out today

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.