-1

我是 Objective-c 的新手,正在开发 ios 应用程序,我正在解析 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];
}
4

1 回答 1

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)

你需要做的是改变你让你的战士出去的方式。您需要显示 2*(current row) 和 (2*currentRow + 1) 而不是在 (current row) 和 (current row + 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];
于 2012-10-07T12:03:41.977 回答