我有一個看起來像這樣JSON數據:如何在iOS中單擊按鈕(目標c)時獲取UITableView中的特定JSON數據?
{
"success": true,
"data": [
{
"playerId": "j_mohammed",
"playerName": "Jason Mohammed",
"playerShortName": "Jason Mohammed",
"PlayingRole": 3,
"Credit": 85,
"Player_Points": 45,
"TeamName": "wi",
"TeamColor": "1"
},
{
"playerId": "s_gabriel",
"playerName": "Shannon Gabriel",
"playerShortName": "ST Gabriel",
"PlayingRole": 4,
"Credit": 80,
"Player_Points": 0,
"TeamName": "wi",
"TeamColor": "1"
},
{
"playerId": "s_hope",
"playerName": "Shai Hope",
"playerShortName": "S Hope",
"PlayingRole": 1,
"Credit": 85,
"Player_Points": 0,
"TeamName": "wi",
"TeamColor": "1"
},
{
"playerId": "e_lewis",
"playerName": "Evin Lewis",
"playerShortName": "Evin Lewis",
"PlayingRole": 3,
"Credit": 85,
"Player_Points": 281,
"TeamName": "wi",
"TeamColor": "1"
},
{
"playerId": "m_hafeez",
"playerName": "Mohammad Hafeez",
"playerShortName": "Mohammad Hafeez",
"PlayingRole": 2,
"Credit": 90,
"Player_Points": 0,
"TeamName": "pak",
"TeamColor": "4"
},
{
"playerId": "f_ashraf",
"playerName": "Fahim Ashraf",
"playerShortName": "Fahim Ashraf",
"PlayingRole": 3,
"Credit": 85,
"Player_Points": 0,
"TeamName": "pak",
"TeamColor": "4"
}
]
}
我已經解析了JSON到我的應用程序。 查看我的應用程序是這樣的:
,因爲它表明,我已經4個按鈕,在視圖的頂部:
我已經採取的TableView在視圖控制器在那裏我已經解析的數據:
這是我做了什麼。現在我的主要問題是,當我點擊按鈕「WK」,當我點擊「BAT」時,擊球手的列表等,我只想在桌面視圖中顯示JSON的門衛列表。我現在完成了解析,來自JSON的數據,並且不知道如何在單擊按鈕時在TableView中顯示選定的數據。
無論球員是投球手,擊球手還是小門將,它都以JSON形式存儲爲"PlayingRole"
。 所以,如果玩家是Wicket keeper its role is:1 All rounder:2 Batsman:3 Bowler:4
這是代碼我做:
NSString *mainurl = [NSString stringWithFormat:@"http://192.168.1.102/redbull11/webservices/api/getMatcheSquad/「];
NSString *playerurl = [NSString stringWithFormat:@"%@%@",mainurl,match_id2];
NSURL *url=[NSURL URLWithString:playerurl];
NSLog(@"mainurl: %@",url);
NSURLSessionConfiguration *csc=[NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *csession = [NSURLSession sessionWithConfiguration:csc delegate:nil delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionDataTask *ctask=[csession dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
getsuccess = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
NSString *success =[getsuccess valueForKey:@"success"];
if([success boolValue] == YES)
{
NSArray *arr =[getsuccess valueForKey:@"data"];
for (NSDictionary *dic2 in arr) {
NSMutableArray *temp =[[NSMutableArray alloc]init];
[temp addObject:[dic2 valueForKey:@"playerShortName"]];
[temp addObject:[dic2 valueForKey:@"Credit"]];
[temp addObject:[dic2 valueForKey:@"Player_Points"]];
[temp addObject:[dic2 valueForKey:@"TeamName"]];
[temp addObject:[dic2 valueForKey:@"PlayingRole"]];
[getLoc addObject:temp];
}}
NSLog(@"%@",getLoc);
[_playerselect reloadData];
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
});
}];
[ctask resume];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
selectTeamCell *cell = [tableView dequeueReusableCellWithIdentifier:@"names" forIndexPath:indexPath];
NSMutableArray *arr =[[NSMutableArray alloc]init];
arr = [getLoc objectAtIndex:indexPath.row];
NSLog(@"%ld",(long)indexPath.row);
cell.playerName.text = [arr objectAtIndex:0];
cell.playerCredit.text = [[arr objectAtIndex:1]stringValue];
cell.playerPoints.text = [[arr objectAtIndex:2]stringValue];
cell.teamName.text = [arr objectAtIndex:3];
return cell;
}
請幫助。先謝謝你。
還有......什麼是你的代碼? –
@Maulik Desai根據您的需求解析json爲batsman創建兩個數組,爲守護者創建兩個數組並添加適當的數據,並相應地獲取它們。 –
@AlejandroIván請參閱我已編輯的問題。 –