Ios 在TableViewController中使用带有JSON数据的NSMutableArray
下午好 我试图从JSON输出在TableViewController中使用NSMutableArray,但我有点迷路了,因为目前我可以将JSON数据存储在NSMutableArray中,我已经使用NSLog检查了内容是否正常,但现在我必须在TableViewController中显示该数据,这就是我迷路的时候 我正在使用另一个使用NSArray的TableViewController示例,但现在我必须为NSMutableArray修改它。如果你能帮我写一些代码或给我看一些例子或教程,我将不胜感激 我知道在我的代码中可能有一些错误,因为我使用的是一个仅使用NSArray的旧示例,但我向您展示的是,因为这是我不知道如何做的,我需要使用NSMutableArray,这就是我请求您帮助的原因 如何在TableViewController中显示NSMutableArrayIos 在TableViewController中使用带有JSON数据的NSMutableArray,ios,objective-c,json,xcode,xcode6,Ios,Objective C,Json,Xcode,Xcode6,下午好 我试图从JSON输出在TableViewController中使用NSMutableArray,但我有点迷路了,因为目前我可以将JSON数据存储在NSMutableArray中,我已经使用NSLog检查了内容是否正常,但现在我必须在TableViewController中显示该数据,这就是我迷路的时候 我正在使用另一个使用NSArray的TableViewController示例,但现在我必须为NSMutableArray修改它。如果你能帮我写一些代码或给我看一些例子或教程,我将不胜感激
- (void)viewDidLoad
{
[super viewDidLoad];
[self fetchJson];
}
-(void)fetchJson {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSString * urlString = [NSString stringWithFormat:@"http://website.com/service.php"];
NSURL * url = [NSURL URLWithString:urlString];
NSData * data = [NSData dataWithContentsOfURL:url];
//NSError * error;
//NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
// I advice that you make your carModels mutable array and initialise it here,before start working with json
//self.carModels = [[NSMutableArray alloc] init];
NSMutableArray *carModels=[[NSMutableArray alloc]init];
NSMutableArray *carMakes=[[NSMutableArray alloc]init];
NSMutableArray *carImages=[[NSMutableArray alloc]init];
@try
{
NSError *error;
NSMutableArray* json = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves
error:&error];
if (error)
{
NSLog(@"%@",[error localizedDescription]);
}
else
{
for(int i=0;i<json.count;i++)
{
NSDictionary * jsonObject = [json objectAtIndex:i];
NSString* imagen = [jsonObject objectForKey:@"imagen"];
[carImages addObject:imagen];
NSDictionary * jsonObject2 = [json objectAtIndex:i];
NSString* user = [jsonObject2 objectForKey:@"user"];
[carMakes addObject:user];
NSDictionary * jsonObject3 = [json objectAtIndex:i];
NSString* images = [jsonObject3 objectForKey:@"date"];
[carModels addObject:images];
}
}
}
@catch (NSException * e)
{
NSLog(@"Exception: %@", e);
}
@finally
{
NSLog(@"finally");
// That's showing the data "1, 2, 3".
NSLog(@"models: %@", carModels);
NSLog(@"makes: %@", carMakes);
NSLog(@"images: %@", carImages);
}
}
);
}
- (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 = [_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];
}
}
-(void)viewDidLoad
{
[超级视图下载];
[自我获取];
}
-(void)fetchJson{
调度队列=调度获取全局队列(调度队列优先级默认为0);
调度异步(队列^{
NSString*urlString=[NSString stringWithFormat:@]http://website.com/service.php"];
NSURL*url=[NSURL URLWithString:urlString];
NSData*data=[NSData dataWithContentsOfURL:url];
//n错误*错误;
//NSMutableArray*json=[NSJSONSerialization JSONObjectWithData:数据选项:针织错误:&错误];
//我建议您在开始使用json之前,将您的carModels可变数组设置为可变数组,并在此处初始化它
//self.carModels=[[NSMutableArray alloc]init];
NSMutableArray*carModels=[[NSMutableArray alloc]init];
NSMutableArray*carMakes=[[NSMutableArray alloc]init];
NSMutableArray*carImages=[[NSMutableArray alloc]init];
@试一试
{
n错误*错误;
NSMutableArray*json=[NSJSONSerialization
JSONObjectWithData:数据
选项:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves
错误:&错误];
如果(错误)
{
NSLog(@“%@,[错误本地化描述]);
}
其他的
{
对于(int i=0;iAdd
获取并解析JSON后。将NSMutableArray*jsonArray;声明为全局
-(void)fetchJson {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSString * urlString = [NSString stringWithFormat:@"http://website.com/service.php"];
NSURL * url = [NSURL URLWithString:urlString];
NSData * data = [NSData dataWithContentsOfURL:url];
//NSError * error;
//NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
// I advice that you make your carModels mutable array and initialise it here,before start working with json
//self.carModels = [[NSMutableArray alloc] init];
@try
{
NSError *error;
[jsonArray removeAllObjects];
jsonArray = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves
error:&error];
}
@catch (NSException * e)
{
NSLog(@"Exception: %@", e);
}
@finally
{
[self.tableView reloadData]
}
}
);
}
要像这样显示TableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [jsonArray 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 = [[jsonArray objectAtIndex:indexPath.row] valueForKey:@"imagen"];
cell.modelLabel.text = [[jsonArray objectAtIndex:indexPath.row] valueForKey:@"user"];
UIImage *carPhoto = [[jsonArray objectAtIndex:indexPath.row] valueForKey:@"date"];
cell.carImage.image = carPhoto;
return cell;
}
- 无论何时更改不同数组中的表数据,只需在方法中添加
[self.tableView reloadData]
- 它将删除旧数据并将新数据显示到您的
表视图中
carModels、carMakes、carImages,这些都是数组?取三个数组需要什么,直接使用json解析数组来显示数据是的,我的数组是carModels、carmages和carMakes。在每一个数组中,我都存储了一个“id”列表、“图像”列表和一个“名称”列表在TableViewController中显示它们。使用单个数组!数组包含字典您忘记在finally块中添加[self.youTableView reloadData]
方法。谢谢@srinivas n,我稍后会尝试。非常感谢。谢谢伙计。您知道我必须在哪里编写代码来显示“加载”之类的“旋转轮子”吗在加载所有内容之前?它是否在viewdidload中?谢谢。在调用fetchJson方法时启动微调器!并在重新加载表视图后停止并移除微调器。再次在tableview单元格中显示图像?这是nsdata格式或url?我在这里发布了一个新线程:您可以检查它吗?谢谢@srinivasN
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [jsonArray 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 = [[jsonArray objectAtIndex:indexPath.row] valueForKey:@"imagen"];
cell.modelLabel.text = [[jsonArray objectAtIndex:indexPath.row] valueForKey:@"user"];
UIImage *carPhoto = [[jsonArray objectAtIndex:indexPath.row] valueForKey:@"date"];
cell.carImage.image = carPhoto;
return cell;
}