2013-02-04 62 views
1

我不斷收到一個錯誤,說我的表格數組是空的。這裏是我的代碼:故事板UITableview與自定義單元格 - 空陣列錯誤

- (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超出界限 空陣列

任何想法爲什麼我的字典總是零,我應該調整我的代碼,使其工作?我基本圖案這對這裏的例子:

http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_afnetworking/

任何幫助,將不勝感激。謝謝!

回答

1

改變這一行

self.testMutableArray = [JSON objectForKey:@"movies"]; 

喜歡的東西:

[self.testMutableArray addObject:(id)[JSON objectForKey:@"movies"]]; 

你不是在你的對象數組增加,你的每一次基本上清除它,並賦予一些對象,可能會或可能對它無效。如果您希望cellForRowAtIndexPath正常工作,則需要將對象連續添加到數組中。

哦,問題不是你的字典是零。這是你試圖抓住不存在的數組的一部分/沒有分配給它的東西,並將它分配給你的字典。用testMutableArray.count檢查你的數組數,看看你實際擁有多少個物體。

+0

謝謝。將嘗試它。更新:它仍然返回相同的錯誤。 ( – jaytrixz

+0

是單個項目或幾個json對象? –

+2

也在cellForRow中,添加此︰if(indexPath.row

相關問題