1

我不断收到错误消息,说我的表数组为空。这是我的代码:

- (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/

4

1 回答 1

1

更改此行

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

类似于:

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

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

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

于 2013-02-05T00:50:58.453 回答