正在从json数组设置cell.textlabel.text



你好,我正在尝试从json中解析数据,并获取要在单元格中显示的值。

这是我的ViewController.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
    IBOutlet UITableView *mainTableView;
    NSMutableData *data;
}
@property (nonatomic, strong) NSArray *category;
@property (nonatomic, strong) NSArray *coursesArray;
@property (nonatomic, strong) NSDictionary *parsedData;
@end

这是我在ViewController.m 中看到的

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    parsedData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    coursesArray = [parsedData valueForKey:@"courses"];
    NSLog(@"coursesArray: %@", coursesArray);
    category = [coursesArray valueForKey:@"category"];
    NSLog(@"%@", category);
}
-(int)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:@"MainCell"];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%@", [category objectAtIndex:indexPath.row]];
    NSLog(@"%@", category);
    return cell;
}

在connectionDidFinishLoading中,我在日志中看到了正确的值,但当我试图在创建单元格之前检查它们时,我会得到null。我在这里做错了什么?

调用

 [mainTableView reloadData];

在您的-connectionDidFinishLoading:方法中。

确保您的类别数组未发布/自动发布。

最新更新