从JSON文件中填写UITableView

时间:2014-08-24 19:14:42

标签: ios objective-c json uitableview

我有来自本地JSON文件的数据,需要填写我的UITableView

之后
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return sports.count;
}

我不确定如何填写- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section + - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

目前我的代码如下所示,你能帮忙吗?谢谢!会根据需要发布任何额外的内容!

    NSArray *sports = JSONDictionary[@"sports"]; //array containing all sports (baseball, football, etc.)

    NSDictionary *baseball = sports[0]; //dictionary containing info about baseball

    NSArray *baseballLeagues = baseball[@"leagues"]; //array containing all leagues for baseball

    NSDictionary *MLB = baseballLeagues[0]; //dictionary for only the MLB league

    NSString *MLBName = MLB[@"name"]; //the full name of the MLB

我正试图在name s中获得棒球联赛UITableViewCell

JSON.json:

{
    "sports": [{
               "name": "baseball",
               "id": 1
               },
               "leagues": [{
                           "name": "Major League Baseball",
                           "abbreviation": "mlb",
                           "id": 10,
                           "uid": "s:1~l:10",
                           "groupId": 9,
                           "shortName": "MLB",
                           "season": {
                           "year": 2014,
                           "type": 2,
                           "description": "regular",
                           "startDate": "2014-03-21T07:00:00Z",
                           "endDate": "2014-09-30T06:59:59Z"
                           },
                           "week": {
                           "number": 23,
                           "startDate": "2014-08-22T07:00:00Z",
                           "endDate": "2014-08-29T06:59:00Z"
                           }
                           }, {
                           "name": "Men's College Baseball",
                           "abbreviation": "college-baseball",
                           "id": 14,
                           "groupId": 14,
                           "shortName": "NCAA Men's Baseball"
                           }]
               },

2 个答案:

答案 0 :(得分:1)

基于评论的编辑 - 以下是您想要做的工作实现,它产生以下结果:

enter image description here

- (void)viewDidLoad {
    [super viewDidLoad];

    NSDictionary *baseball = @{
                               @"name": @"baseball",
                               @"leagues": @[@{@"name": @"Major League Baseball"}, @{@"name": @"Men's College Baseball"}]
                               };

    NSDictionary *football = @{
                               @"name": @"football",
                               @"leagues": @[@{@"name": @"National Football League"}, @{@"name": @"NCAA Football"}]
                               };

    NSDictionary *basketball = @{
                               @"name": @"basketball",
                               @"leagues": @[@{@"name": @"National Basketball Assoc."}, @{@"name": @"Women's National Basketball Assoc"}, @{@"name": @"NCAA Basketball"}]
                               };

    // instead of hard-coding in everything like I just did for testing, this should be
    // your JSON
    _sports= @[baseball, football, basketball];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // return the number of sports, as each sport gets its own section
    return _sports.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // get the sport for this section
    NSDictionary *sport = _sports[section];

    // get the array of leagues for that sport
    NSArray *leaguesForSport = sport[@"leagues"];

    // return the number of leagues in the leagues array for this section
    return leaguesForSport.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // get the sport for this section
    NSDictionary *sport = _sports[indexPath.section];

    // get the sport's leagues array
    NSArray *leaguesForSport = sport[@"leagues"];

    // get the league for this row
    NSDictionary *league = leaguesForSport[indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    // if we couldn't reuse a cell, create one
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    // now customize the cell based on the league information
    cell.textLabel.text = league[@"name"];

    // return the cell
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSDictionary *sport = _sports[section];
    return sport[@"name"];
}

@end

答案 1 :(得分:1)

这就是我实现这个

的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [sports[section][@"leagues"] count]; //returns the number of leagues in each sport
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *leagueName = sports[indexPath.section][@"leagues"][indexPath.row][@"name"];

    static NSString *cellID = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (!cell)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    cell.textLabel.text = leagueName;
    return cell;
}

此解决方案将设置表格视图,以便每项运动都有自己的部分,在这些部分中的单元格包含该运动中每个联赛的名称。