0

我的计划是从 web 服务中获取一个 json,并将结果显示在一个分组的 tableview 中,其中包含来自 json 的部分。

我的json到目前为止:

{
"Building 1":[{"title":"Room 1","id":"1"},{"title":"Room 2","id":"11"},{"title":"Room 3","id":"12"}],
"Building 2":[{"title":"Room 1","id":"37"},{"title":"Room 2","id":"23"}],
"Building 3":[{"title":"Room 1","id":"9"},{"title":"Room 2","id":"3"}]
}

我可以通过以下方式获取并写出来

-(void)connectionDidFinishLoading: (NSURLConnection *)connection
{
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];
    NSLog(@"array contains %@",json);

    [self.tableView reloadData];
}

但是如何做剩下的并在带有部分标题的表格视图中显示它:Building 1 Room 1 Room 2 Room 3 Building 2 Room 1

等等?

4

1 回答 1

0

创建一个动态原型 UITableviewcell(或具有所需部分的静态单元格)并将数据从数组加载到 UITableviewCells。我们可以使用 Tableview 委托方法将数据加载到 uitableviewcells 中。

示例代码:

    - (NSInteger)numberOfSectionsInTableView: (UITableView *)tableView
    {
        return 1;
    }

    - (NSInteger)tableView: (UITableView *)tableView numberOfRowsInSection: (NSInteger)section
    {
        //NSLog(@"inputplayerslist count=%d",[playerPositionList count]);
        return [playerPositionList count];
    }




    - (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
    {
        AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
        UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ChooseAPlayer"];

        NSString *ImagName =[[appDelegate.userSelection objectForKey:@"sportValue"] stringByAppendingString:@"Player.png"];
        UIImageView *playerGraphicImage = (UIImageView *)[self.view viewWithTag:31];
        [playerGraphicImage setImage:[UIImage imageNamed:ImagName]];

        UILabel *lblName = (UILabel *)[cell viewWithTag:101];
        [lblName setText:[playerPositionList objectAtIndex:[indexPath row]]];
        UILabel *Teams = (UILabel *)[cell viewWithTag:103];
        [Teams setText:[playerTeamidList objectAtIndex:[indexPath row]]];

        UILabel *AwayTeams = (UILabel *)[cell viewWithTag:104];
        [AwayTeams setText:[playerOPTeamidList objectAtIndex:[indexPath row]]];

        UILabel *PlayersPrice = (UILabel *)[cell viewWithTag:105];
        [PlayersPrice setText:[playerPriceList objectAtIndex:[indexPath row]]];
}
于 2013-02-25T09:48:04.443 回答