2012-10-07 2 views
-1

나는 객관적인 -c로 새롭고 json 배열을 파싱하고 테이블 셀에 표시하는 ios 응용 프로그램에서 작업합니다. 나는 하나의 셀에 2 JSON 배열 값을 표시 할 문제가있다하지만 난 제대로 값을 받고 있지 않다, 여기 여기objective-c의 단일 테이블 셀에 두 개의 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"; 
} 

내가 두 diffrent 배열에서 전투기 이름을 표시 할 내 JSON 배열입니다 sigle 세포, 예. kaushik sen 대 kultar singh 아가미하지만 셀에 대체 플레이어 이름이 나타납니다. 여기에 나의 객관적인 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]; 
} 

답변

2

문제는 셀에 이름을 쓸 때입니다. 당신이 모든 행을 구성하도록 요청 : cellForRowAtIndexPath : 각 행의 경우, 현재 행을 받고,하고있는 현재 행은 + 1

그래서 당신이 전투기

있는 tableView 이후

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

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)의 , 당신은 (로 CurrentRow에게 + 1 * 2)

Row 0: 0 and 1 (John vs Bob) 
Row 1: 2 and 3 (Bill vs Carl) 
Row 2: 4 and 5 (Tom vs Mark) 

그래서 해결하기 위해 2 * (현재 행) 등을 표시해야 4자를 추가하여 코드 :

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