2013-02-25 41 views
0

我的計劃是在與來自JSON部分已分組的tableview從web服務獲取一個JSON和顯示結果。從JSON來UiTableViewControll

我的JSON SOFAR:

{ 
"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]; 
} 

但如何做休息,並顯示在與節頭一個實現代碼如下: 1號樓 房間1 室2 室3 2號樓 房間1

等等?

+0

你嘗試過什麼?有教程的色調有關使用的UITableView /的UITableViewController – 2013-02-25 09:03:41

+0

我曾嘗試:HTTP://stackoverflow.com/questions/12703912/how-to-set-numberofsectionsintableview-titleforheaderinsection-using-dynamic-js但它的所有關於valueForKey與我不有。 – user2106600 2013-02-25 10:16:24

+0

我只是需要幫助,指導我如何使用此進行正確的方向。如何將NSDictionary中轉換的東西的例子我能在numberOfSectionsInTableView和titleForHeaderInSection使用 – user2106600 2013-02-25 10:59:25

回答

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]]]; 
}