故事板UITableview与自定义单元格 - 空数组错误

时间:2013-02-04 23:54:11

标签: objective-c xcode uitableview storyboard afnetworking

我一直收到错误,说我的表数组是空的。这是我的代码:

- (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 个答案:

答案 0 :(得分:1)

更改此行

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

类似于:

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

你没有将对象添加到数组中,每次都基本清除它并为它分配一些可能有效或无效的对象。如果希望cellForRowAtIndexPath正常工作,则需要将对象连续添加到数组中。

哦,问题不在于你的字典是零。这是你试图抓住你不存在的数组的一部分/没有分配给它的东西并将其分配给你的字典。使用testMutableArray.count检查您的数组计数,以查看您实际拥有的对象数。