Iphone 如何在objective-c中的单个表格单元格中显示两个json数组值
我是objective-c新手,正在开发ios应用程序,解析json数组并将其显示在表格单元格中。我有一个问题,我想在一个单元格中显示2个json数组值,但我没有正确获取这些值,这是我的json数组Iphone 如何在objective-c中的单个表格单元格中显示两个json数组值,iphone,objective-c,ios,json,tablecell,Iphone,Objective C,Ios,Json,Tablecell,我是objective-c新手,正在开发ios应用程序,解析json数组并将其显示在表格单元格中。我有一个问题,我想在一个单元格中显示2个json数组值,但我没有正确获取这些值,这是我的json数组 { "event_id" = 7; "fighter_id" = 26; "fighters_name" = "Kaushik Sen"; "fighters_photo" = "Kaushik.jpg"; "match_id" = 28; "
{
"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";
}
在这里,我想展示单格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格。这是我的目标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];
}
-(void)viewDidLoad
{
[超级视图下载];
[自我匹配列表];
}
-(无效)匹配列表{
NSURLRequest*request=[NSURLRequest requestWithURL:[NSURL URLWithString:@]http://example.com/appleapp/eventDetails.php"]];
n错误*错误;
//执行php代码的代码
NSData*data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
NSDictionary*json=[NSJSONSerialization JSONObjectWithData:数据选项:NSJSONReadingMutableContainers错误:nil];
NSMutableArray*matchListArray=[[NSMutableArray alloc]init];
matchListArray=[json objectForKey:@“产品”];
ArrayOfficerName=[[NSMutableArray alloc]init];
arrOfMatchId=[[NSMutableArray alloc]init];
对于(int i=0;i而言,问题在于将名称写入单元格时。
对于每一行,您将获得当前行和当前行+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*(当前行)和(2*当前行+1),而不是在(当前行)和(当前行+1)显示战斗机
因此,要通过添加4个字符来修复代码:
cell.lblPlayer1.text = [arrayOfFighterName objectAtIndex:2*indexPath.row];
cell.lblPlayer2.text = [arrayOfFighterName objectAtIndex:2*indexPath.row +1];
cell.lblPlayer1.text = [arrayOfFighterName objectAtIndex:2*indexPath.row];
cell.lblPlayer2.text = [arrayOfFighterName objectAtIndex:2*indexPath.row +1];