如何在objective-c中的单个表格单元格中显示两个json数组值

时间:2012-10-07 11:44:00

标签: iphone objective-c ios json tablecell

我是objective-c的新手,在ios app上工作,我在解析json数组并在表格单元格中显示它。我有问题,我想在单个单元格中显示2个json数组值,但我没有正确获取值,这是我的json数组

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

这里我想显示来自单元格中两个不同阵列的战斗机名称,例如。 kaushik sen vs kultar singh gill,但我在单元格中获得了替代球员的名字。这是我的目标c代码。

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

1 个答案:

答案 0 :(得分:2)

问题是当您将名称写入单元格时。 对于每一行,您将获得当前行,并且当前行为+ 1.

所以想象你有战士

0. John
1. Bob
2. Bill
3. Carl
4. Tom
5. Mark

由于tableView:cellForRowAtIndexPath:要求你配置每个行,你显示的是:

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)

你需要做的是改变你的战士出局方式。 而不是在(当前行)和(当前行+ 1)显示战士,你需要显示2 *(当前行)和(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)

因此,要通过添加4个字符来修复代码:

cell.lblPlayer1.text =  [arrayOfFighterName objectAtIndex:2*indexPath.row];
cell.lblPlayer2.text =  [arrayOfFighterName objectAtIndex:2*indexPath.row +1];