-1

私はobjective-cが初めてで、json配列を解析してテーブルセルに表示しているiosアプリに取り組んでいます。1 つのセルに 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";
}

ここでは、単一セル内の 2 つの異なる配列から戦闘機の名前を表示したいと思います。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)

あなたがする必要があるのはあなたがあなたの戦闘機を出す方法を変えることです。(現在の行)と(現在の行+ 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];
于 2012-10-07T12:03:41.977 に答える