Reputation: 4079
I keep getting an error saying my table array is empty. Here's my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// Set navigation bar image
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"header.png"]
forBarMetrics:UIBarMetricsDefault];
spinnerActivity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
spinnerActivity.hidesWhenStopped = YES;
spinnerBarButton = [[UIBarButtonItem alloc] initWithCustomView:spinnerActivity];
self.navigationItem.rightBarButtonItem = spinnerBarButton;
[spinnerActivity startAnimating];
self.testString = [[NSString alloc] init];
self.testMutableArray = [[NSMutableArray alloc] init];
self.myTableView = [[UITableView alloc] init];
[self.view addSubview:self.myTableView];
[self getMoviesInformation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidUnload
{
[self setMenuBarButtonItem:nil];
[super viewDidUnload];
}
#pragma mark - Custom Methods
- (void)getMoviesInformation
{
NSURL *url = [NSURL URLWithString:@"http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=6844abgw34rfjukyyvzbzggz&q=The+Man+With+The+Iron+Fists&page_limit=1"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
self.testMutableArray = [JSON objectForKey:@"movies"];
NSLog(@"Return String: %@", self.testMutableArray);
[self.myTableView reloadData];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
}
#pragma mark - Tableview Methods
#pragma mark DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"movieTableCell";
MovieTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[MovieTableCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
NSDictionary *dict = [self.testMutableArray objectAtIndex:indexPath.row];
NSLog(@"Dict: %@", dict);
cell.titleLabel.text = @"test";
NSLog(@"Cell TitleLabel: %@", cell.titleLabel.text);
return cell;
}
#pragma mark Delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 150;
}
Here's the error message I'm encountering:
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array
Any idea why my dictionary is always nil and what I should adjust in my code to make it work? I basically patterned this to the example here:
http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_afnetworking/
Upvotes: 1
Views: 241
Reputation: 13549
Change this line
self.testMutableArray = [JSON objectForKey:@"movies"];
to something like:
[self.testMutableArray addObject:(id)[JSON objectForKey:@"movies"]];
Your not adding in your objects to the array, your basically clearing it every time and assigning some object, that may or may not valid, to it. You need to consecutively add objects into the array if you want cellForRowAtIndexPath to work correctly.
Oh and the problem is not that your dictionary is nil. It's that your trying to grab a part of your array that doesn't exist/ hasn't had something assigned to it and assign that to your dictionary. Check your array count with testMutableArray.count
to see how many objects you actually have in it.
Upvotes: 1