【问题标题】:Setting cell.textlabel.text from json array从 json 数组设置 cell.textlabel.text
【发布时间】:2013-03-12 08:23:58
【问题描述】:

您好,我正在尝试从 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。 我在这里做错了什么?

【问题讨论】:

    标签: objective-c json uitableview ios6 nsarray


    【解决方案1】:

    打电话

     [mainTableView reloadData];
    

    在您的 -connectionDidFinishLoading: 方法中。

    【讨论】:

      【解决方案2】:

      确保您的类别数组未释放/自动释放。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-29
        相关资源
        最近更新 更多