2017-04-11 20 views
0

我有一個看起來像這樣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個按鈕,在視圖的頂部:

enter image description here

我已經採取的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; 
} 

請幫助。先謝謝你。

+0

還有......什麼是你的代碼? –

+0

@Maulik Desai根據您的需求解析json爲batsman創建兩個數組,爲守護者創建兩個數組並添加適當的數據,並相應地獲取它們。 –

+0

@AlejandroIván請參閱我已編輯的問題。 –

回答

0

我已經解決了我的問題這裏是我的代碼所做的:

@implementation selectTeam{ 
int flag; 
} 
- (void)viewDidLoad { 
[super viewDidLoad]; 
flag = 1; 
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]; 
playerrole = [[dic2 valueForKey:@"PlayingRole"]stringValue]; 
NSLog(@"role: %@",playerrole); 
if ([playerrole isEqual:@"1"]) { 
[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"]]; 
[wkeeper addObject:temp]; 
} 
else if ([playerrole isEqual:@"2"]) { 
[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"]]; 
[allrounder addObject:temp]; 
} 
else if ([playerrole isEqual:@"3"]) 
{ [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"]]; 
[batsman addObject:temp]; 
} 
else if ([playerrole isEqual:@"4"]) 
{ 
[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"]]; 
[bowler addObject:temp]; 
}}} 
[_playerselect reloadData]; 
[MBProgressHUD hideHUDForView:self.view animated:YES]; 
}); 
}); 
}]; 
[ctask resume]; 



- (IBAction)wicketkeeper:(id)sender { 
    flag=1; 
_pickplayers.text= @"PICK 1 WICKET-KEEPER"; 
    [_playerselect reloadData];  
} 
- (IBAction)batsman:(id)sender { 
    flag=3; 
    _pickplayers.text= @"PICK 3-5 BATSMEN"; 
    [_playerselect reloadData]; 
} 
- (IBAction)allrounder:(id)sender { 
    flag=2; 
    _pickplayers.text= @"PICK 1-3 ALL-ROUNDERS"; 
    [_playerselect reloadData]; 
} 
- (IBAction)bowler:(id)sender { 
    flag=4; 
    _pickplayers.text= @"PICK 3-5 BOWLERS"; 
    [_playerselect reloadData];  
} 



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    selectTeamCell *cell = [tableView dequeueReusableCellWithIdentifier:@"names" forIndexPath:indexPath]; 
    NSMutableArray *arr =[[NSMutableArray alloc]init]; 
if (flag==1){ 
     arr = [wkeeper objectAtIndex:indexPath.row]; 
     NSLog(@"%ld",(long)indexPath.row); 
    } 
    else if (flag==2){ 
     arr = [allrounder objectAtIndex:indexPath.row]; 
     NSLog(@"%ld",(long)indexPath.row); 
    } 
    else if (flag==3){ 
     arr = [batsman objectAtIndex:indexPath.row]; 
     NSLog(@"%ld",(long)indexPath.row); 
    } 
    else{ 
     arr = [bowler 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; 
} 
+0

我很高興你設法解決你的問題。作爲一個建議,你可以在Objective-C中使用新的語法來清理你的代碼。例如,不用'[someArray objectAtIndex:number]',你可以簡單地使用'someArray [number]'來產生相同的效果。這也適用於'NSDictionary',你可以在其中執行'someDictionary [@「key」]'而不是'[someDictionary valueForKey:@「key」]''。快樂的編碼! –

+0

爲了初始化'NSArray','NSNumber'和'NSDictionary',你可以使用[literal syntax](https://www.bignerdranch.com/blog/objective-c-literals-part-1/)。 –

+0

@AlejandroIván謝謝你的建議兄弟!我已經完成了你的建議!謝謝你的幫助!! –

0

使用此謂詞來過濾數據

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"PlayingRole == 1"]; 
NSArray *arrWicketKeeper = [arrMain filteredArrayUsingPredicate:predicate]; 
1

如果你只想顯示檢票keepers.This是隻顯示在表格視圖單元格檢票飼養

1)存儲在您迴應數組。

NSArray *yourArray =[getsuccess valueForKey:@"data"];//please declare your array globally 

然後

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
return yourArray.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

selectTeamCell *cell = [tableView dequeueReusableCellWithIdentifier:@"names" forIndexPath:indexPath]; 


NSDictionary *dictYourDict; 
dictYourDict=[yourArray objectAtIndex:indexPath.row]; 

cell.yourLabel.text=[dictYourDict objectForkEy : @"PlayingRole"]; 

return cell; 
} 
+0

感謝您的努力。正如你所說的那樣,它正在顯示wicketkeepers。但是當我點擊不同的按鈕時,我想改變wkeeper,擊球手,擊球手和allrounder。 –

+0

然後你可以做什麼是另一個屬性,它指出你想要顯示'@property(strong,nonatomic)NSString * typeOfData'的數據類型,並在'tableView:cellForRowAtIndexPath:'中使用一個信息或另一個信息。最簡單的方法是爲每種選擇實際有不同的數組。你的按鈕的'IBAction'應該定義該屬性並調用'[tableView reloadData];' –

+0

是的,我知道隊友。我只是告訴你通往那的路。您必須在每次點擊中設置一個變量並在ibaction中調用它。並重新加載你的表格視圖。 –

相關問題