无法使用NSDictionary的内容填充UITableViewCells

时间:2014-06-22 20:42:45

标签: ios objective-c json uitableview

我已将iOS应用设置为从某些Parse云代码接收JSON。根据我在下图中看到的内容,JSON由名为NSDictionary的{​​{1}}组成,其中包含3个名为matchCenterDictionary的NSArray对象,每个对象都包含Top 3有4个键/值对。

我想要做的是在我的NSDictionary中为UITableView中显示的Top 3的每个实例创建一个新部分,并使用以下内容填充每个相应部分的3个单元格4个键/值对信息(标题,价格,imgurl等)。

当我运行模拟时,应用程序崩溃并指向:

matchCenterDictionary

作为问题区域,这意味着我没有正确引用它。代码,错误消息和屏幕截图如下。

MatchCenterViewController.m:

cell.textLabel.text = [[[_matchCenterDictionary objectForKey:@"Top 3"] objectAtIndex:0] objectForKey:@"Title"];

错误讯息:

#import "MatchCenterViewController.h"
#import <UIKit/UIKit.h>

@interface MatchCenterViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *matchCenter;
@end

@implementation MatchCenterViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.matchCenter = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewCellStyleSubtitle];
    self.matchCenter.frame = CGRectMake(0,50,320,self.view.frame.size.height-100);
    _matchCenter.dataSource = self;
    _matchCenter.delegate = self;
    [self.view addSubview:self.matchCenter];

    self.matchCenterDictionary = [[NSDictionary alloc] init];
}

- (void)viewDidAppear:(BOOL)animated
{
    self.matchCenterDictionary = [[NSDictionary alloc] init];

    [PFCloud callFunctionInBackground:@"MatchCenterTest"
                       withParameters:@{
                                        @"test": @"Hi",
                                        }
                                block:^(NSDictionary *result, NSError *error) {

                                    if (!error) {
                                        self.matchCenterDictionary = [result objectForKey:@"MatchCenter"];
                                        [_matchCenter reloadData];

                                        NSLog(@"Result: '%@'", result);
                                    }
                                }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _matchCenterDictionary.count;
}

//the part where i setup sections and the deleting of said sections

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 21.0f;
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 21)];
    headerView.backgroundColor = [UIColor lightGrayColor];

//    _searchTerm = [[self.matchCenterArray firstObject] objectForKey:@"Search Term"];

    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 0, 250, 21)];
//    headerLabel.text = [NSString stringWithFormat:@"%@", searchTerm];
//    headerLabel.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
//    headerLabel.textColor = [UIColor whiteColor];
    headerLabel.backgroundColor = [UIColor lightGrayColor];
    [headerView addSubview:headerLabel];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.tag = section + 1000;
    button.frame = CGRectMake(300, 2, 17, 17);
    [button setImage:[UIImage imageNamed:@"xbutton.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:button];
    return headerView;
}



- (IBAction)deleteButtonPressed:(UIButton *)sender {
    NSLog(@"Search Term: '%@'", _searchTerm);

    [PFCloud callFunctionInBackground:@"deleteFromMatchCenter"
                       withParameters:@{
                                        @"searchTerm": _searchTerm,
                                       }
                                block:^(NSDictionary *result, NSError *error) {

                                    if (!error) {
                                        NSLog(@"Result: '%@'", result);
                                    }
                                }];
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     return 3;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Initialize cell
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        // if no cell could be dequeued create a new one
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }



    // populate dictionary with results

    //NSDictionary *matchCenterDictionary= [_matchCenterArray objectAtIndex:indexPath.row];

    // title of the item
    cell.textLabel.text = [[[_matchCenterDictionary objectForKey:@"Top 3"] objectAtIndex:0] objectForKey:@"Title"];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:12];

    // price of the item
    cell.detailTextLabel.text = [NSString stringWithFormat:@"$%@", [[[_matchCenterDictionary objectForKey:@"Top 3"] objectAtIndex:0] objectForKey:@"Price"]];
    cell.detailTextLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];

    // image of the item
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[[_matchCenterDictionary objectForKey:@"Top 3"] objectAtIndex:0] objectForKey:@"Image URL"]]];
    [[cell imageView] setImage:[UIImage imageWithData:imageData]];
    //imageView.frame = CGRectMake(45.0,10.0,10,10);

    return cell;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

JSON结构:

enter image description here

1 个答案:

答案 0 :(得分:1)

再次查看截图。

你有一个数组 词典

_matchCenterDictionary应为NSArray

更改

cell.textLabel.text = [[[_matchCenterDictionary objectForKey:@"Top 3"]
                                            objectAtIndex:0] objectForKey:@"Title"];

cell.textLabel.text = [[[_matchCenterArray  objectAtIndex:0] 
                                      objectForKey:@"Top 3"] objectForKey:@"Title"];

或更短

cell.textLabel.text = _matchCenterArray[0][@"Top 3"][@"Title"];

当JSON过于嵌套时,您可以将其分解以使其更清晰

NSDictionary *top3 = _matchCenterArray[0];
NSString *title = top3[@"Title"];
cell.textLabel.text = title;