【问题标题】:how to display two json array values in single table cell in objective-c如何在objective-c的单个表格单元格中显示两个json数组值
【发布时间】:2012-10-07 11:43:59
【问题描述】:

我是 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];
}

【问题讨论】:

    标签: iphone objective-c ios json tablecell


    【解决方案1】:

    问题是当您将名称写入单元格时。 对于每一行,您将获得当前行,并且当前行 + 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];
    

    【讨论】:

      猜你喜欢
      • 2020-08-20
      • 2015-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      • 2015-04-30
      • 2017-09-18
      • 2018-04-13
      相关资源
      最近更新 更多