我不断收到错误消息,说我的表数组为空。这是我的代码:
- (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;
}
这是我遇到的错误消息:
* 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“* -[__NSArrayM objectAtIndex:]:空数组的索引 0 超出范围
知道为什么我的字典总是 nil 以及我应该在我的代码中调整什么以使其工作吗?我基本上将其设计为此处的示例:
http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_afnetworking/