how to display two json array values in single table cell in objective-c
-
05-07-2021 - |
Domanda
I am new in objective-c and working on ios app where i am parsing json array and displaying it in table cell. I have the problem that i want to display 2 json array values in single cell but i am not getting the values properly, here is my json array
{
"event_id" = 7;
"fighter_id" = 26;
"fighters_name" = "Kaushik Sen";
"fighters_photo" = "Kaushik.jpg";
"match_id" = 28;
"match_type" = Bantamweight;
"profile_link" = "link";
}
{
"event_id" = 7;
"fighter_id" = 21;
"fighters_name" = "Kultar Singh Gill";
"fighters_photo" = "Kultar.jpg";
"match_id" = 27;
"match_type" = "Welterweights Main Event";
"profile_link" = "link";
}
here i want to show the fighter name from two diffrent arrays in sigle cell, eg. kaushik sen vs kultar singh gill, but i am getting alternate player names in cells. here is my objective c code.
- (void)viewDidLoad
{
[super viewDidLoad];
[self MatchList];
}
-(void)MatchList {
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/appleapp/eventDetails.php"]];
NSError *error;
//code to execute php code
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSMutableArray *matchListArray = [[NSMutableArray alloc]init];
matchListArray = [json objectForKey:@"products"];
arrayOfFighterName=[[NSMutableArray alloc] init];
arrOfMatchId = [[NSMutableArray alloc] init];
for( int i = 0; i<[matchListArray count]; i++){
// NSLog(@"%@", [matchListArray objectAtIndex:i]);
arrayOfFighterName[i]=[[matchListArray objectAtIndex:i] objectForKey:@"fighters_name"];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [arrayOfFighterName count]/2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell = (MyFighterCell *)[tableview dequeueReusableCellWithIdentifier:kCellIdentifier];
select = indexPath.row;
if(cell == nil){
cell = [[[MyFighterCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier] autorelease];
[cellOwner loadMyNibFile:kCellIdentifier];
cell = (MyFighterCell *)cellOwner.cell;
}
cell.lblPlayer1.text = [arrayOfFighterName objectAtIndex:indexPath.row];
cell.lblPlayer2.text = [arrayOfFighterName objectAtIndex:indexPath.row +1];
}
Soluzione
The problem is when you are writing the names to the cell. For each row, you are getting the current row, and the current row + 1.
So imagine you have fighters
0. John
1. Bob
2. Bill
3. Carl
4. Tom
5. Mark
Since the tableView:cellForRowAtIndexPath: asks you to configure every row, what you are displaying is this:
Row 0: display 0 and 1 (John vs Bob)
Row 1: display 1 and 2 (Bob vs Bill)
Row 2: display 2 and 3 (Bill vs Carl)
What you need to do is change the way you get your fighters out. Instead of displaying the fighters at the (current row) and (current row + 1), you need to be displaying 2*(current row) and (2*currentRow + 1)
Row 0: 0 and 1 (John vs Bob)
Row 1: 2 and 3 (Bill vs Carl)
Row 2: 4 and 5 (Tom vs Mark)
So, to fix your code by adding 4 characters:
cell.lblPlayer1.text = [arrayOfFighterName objectAtIndex:2*indexPath.row];
cell.lblPlayer2.text = [arrayOfFighterName objectAtIndex:2*indexPath.row +1];