Ios 使用JSON(数据库数据)的TableView导航
下午好 我使用了以下教程来创建TableView导航:但通过该示例,我可以显示静态信息 现在我需要显示我的数据库中的图像和文本,我需要一些帮助来做到这一点 目前,这是我的CarTableViewController.mIos 使用JSON(数据库数据)的TableView导航,ios,json,xcode,uitableview,xcode6,Ios,Json,Xcode,Uitableview,Xcode6,下午好 我使用了以下教程来创建TableView导航:但通过该示例,我可以显示静态信息 现在我需要显示我的数据库中的图像和文本,我需要一些帮助来做到这一点 目前,这是我的CarTableViewController.m #import "CarTableViewController.h" #import "CarTableViewCell.h" #import "CarTableViewController.h" #import "CarDetailViewController.h" @imp
#import "CarTableViewController.h"
#import "CarTableViewCell.h"
#import "CarTableViewController.h"
#import "CarDetailViewController.h"
@implementation CarTableViewController
@synthesize carMakes = _carMakes;
@synthesize carModels = _carModels;
@synthesize carImages = _carImages;
- (void)viewDidLoad
{
[super viewDidLoad];
self.carMakes = [[NSArray alloc]
initWithObjects:@"Chevy",
@"BMW",
@"Toyota",
@"Volvo",
@"Smart", nil];
self.carModels = [[NSArray alloc]
initWithObjects:@"Volt",
@"Mini",
@"Venza",
@"S60",
@"Fortwo", nil];
self.carImages = [[NSArray alloc]
initWithObjects:@"chevy_volt.jpg",
@"mini_clubman.jpg",
@"toyota_venza.jpg",
@"volvo_s60.jpg",
@"smart_fortwo.jpg", nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.carModels count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"carTableCell";
CarTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CarTableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.makeLabel.text = [self.carMakes
objectAtIndex: [indexPath row]];
cell.modelLabel.text = [self.carModels
objectAtIndex:[indexPath row]];
UIImage *carPhoto = [UIImage imageNamed:
[self.carImages objectAtIndex: [indexPath row]]];
cell.carImage.image = carPhoto;
return cell;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"ShowCarDetails"])
{
CarDetailViewController *detailViewController =
[segue destinationViewController];
NSIndexPath *myIndexPath = [self.tableView
indexPathForSelectedRow];
detailViewController.carDetailModel = [[NSArray alloc]
initWithObjects: [self.carMakes
objectAtIndex:[myIndexPath row]],
[self.carModels objectAtIndex:[myIndexPath row]],
[self.carImages objectAtIndex:[myIndexPath row]],
nil];
}
}
@end
我需要将我数据库中的信息(作者、日期和图片)放入carMakes、carModels和carImages以显示它。我可以用JSON从我的PHP文件中获取值,但是我需要一些关于NSArray with JSON结果的帮助,因为我从未使用过它
我该怎么做
这是我的JSON结果:
[{"id":"15","user":"1","image":"http:\/\/farmaventas.es\/images\/farmaventaslogo.png","date":"2014-09-13"}]
提前谢谢
NSMutableArray* dataSource = [[NSMutableArray alloc] init];
NSMutableDictionary* chevyVolt = [[NSMutableDictionary alloc] init];
chevyVolt[@"Make"] = @"Chevy";
chevyVolt[@"Model"] = @"Volt";
chevyVolt[@"Image"] = @"chevy_volt.jpg";
dataSource[0] = chevyVolt;
NSMutableDictionary* clubman = [[NSMutableDictionary alloc] init];
-- etc --
牢房内--
当您收到JSON数据时,您必须决定它属于哪一行。如果您不知何故知道ID15代表雪佛兰伏特,那么您可能会将相应JSON元素中的信息复制到雪佛兰伏特的字典中。(或者,如果您等待接收JSON以添加雪佛兰Volt行(似乎更可能),您可以如上所述为Volt构建字典,并将其添加到数据源数组的末尾,使用JSON中添加的信息。)您的数据组织错误。每个节有一个数组,数组的元素是字典。然后你就可以解析你的JSON并把它放在那里(尽管通常最好有一个例程,将每一行的数据复制到一个新的字典中,以便稍微整理一下)。你能给我举个例子吗?谢谢@Hot LicksHi Hot Licks。我发现自己遇到了类似的情况,我需要帮助,因为我用一个定制的PHP文件加载信息,然后以JSON格式输出类似JulienKP的信息。我怎么能继续?我必须复制你的代码?谢谢@Hot-Licks。谢谢@Hot-Links,但我也想知道在我显示JSON结果后我该做什么,因为我有点迷路了。你能给我举一个例子说明如何对我的JSON进行匹配吗?@JulienKP-我不知道你打算如何将JSON数据与汽车数据进行匹配。从技术上讲,这并不难,但你需要知道你希望事情如何相关。
NSMutableDictionary* cellData = dataSource[[indexPath row]];
cell.makeLabel.text = cellData[@"Make"];
cell.modelLabel.text = cellData[@"Model"];
-- etc --