2013-02-04 2 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; 
} 

I가 발생하고있어 오류 메시지가 있어요 :

응용 프로그램을 종료

* 인해 캐치되지 않는 예외 'NSRangeException', 이유에 : 여기 내 코드는 '* - [__ NSArrayM objectAtIndex :] : 인덱스 0 범위를 넘어서 빈 배열

왜 내 사전은 항상 nil이고 어떤 식으로 작동하도록하려면 어떻게해야합니까? 기본적으로 내가 여기 예제에이 패턴 :

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

어떤 도움

주시면 감사하겠습니다. 감사!

답변

1

변경 같은이 라인

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

:

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

당신의 배열로 객체에 추가하지, 당신은 기본적으로 할 수 그것을마다 지우고 일부 개체를 할당하는 수도 있고 유효하지 않습니다. cellForRowAtIndexPath가 올바르게 작동하게하려면 배열에 객체를 연속적으로 추가해야합니다.

아, 문제는 사전이 0이 아니라는 것입니다. 그것은 존재하지 않는 배열의 일부를 잡으려고 시도하거나 할당 된 것을 가지지 않고 사전에 할당하는 것입니다. 배열 수를 testMutableArray.count으로 확인하여 실제로 얼마나 많은 객체가 있는지 확인하십시오.

+0

감사합니다. 그것을 시도 할 것이다. 업데이트 : 여전히 동일한 오류를 반환합니다. : – jaytrixz

+0

json 개체의 단일 항목 또는 여러 항목 –

+2

또한 cellForRow에 다음을 추가합니다. if (indexPath.row

관련 문제