获取json对象,然后分配给uitable视图 [英] getting json object and then assign to uitable view

查看:111
本文介绍了获取json对象,然后分配给uitable视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我有一个表格视图.我想做的是这样的: 通过网络获取数据,将其存储在数组中,然后根据每个索引的值,相应的行获取背景.

Well in my app I have a tableview. What i want to do is this: fetch data over the web, store them in an array and then depending of the value of each index the corresponding row to get a background.

我的意思是: 如果data [0] = red --->第0行的图像将为红色,否则为绿色.

What I mean is this: if data[0]=red---> image of row 0 will be red, else green.

我设法下载了数据,但是问题是首先分配了图像,然后发生了http请求.

I have managed to download the data, but problem is that images are first assigned and then happens the http request.

例如: 这是我用于获取数据并将其存储到数组的代码:

For example: This is my code for getting data and storing them to array:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"connectionDidFinishLoading");
    NSLog(@"Succeeded! Received %d bytes of data",[self.responseData length]);

    // convert to JSON
    NSError *myError = nil;
    NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];

    colours== [NSMutableArray array];


    NSString *parsed_data=[res objectForKey:@"data_1"];
    NSLog(@"getting colour : %@",parsed_data);
    [colours addObject:parsed_data];
    .....
    [self.mapMain reloadData];
}

其中mapMain是我的IBoutlet. 并设置背景:

where mapMain is my IBoutlet. and for setting background:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier=@"SimpleTableCell";
    //this is the identifier of the custom cell
    SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    NSLog(@"Colour at %d is %@ ",indexPath.row,[colours objectAtIndex:indexPath.row]);


    if ([@"red" isEqualToString:[colours objectAtIndex:indexPath.row]]) {
        cell.mainImage.image = [UIImage imageNamed:[centersimages_red objectAtIndex:indexPath.row]];
    } else{
    cell.mainImage.image = [UIImage imageNamed:[centersimages_green objectAtIndex:indexPath.row]];
    }

    return cell;
}

其中centerimages是带有我的.pngs名称的表.

where centerimages are tables with my .pngs names.

这是我的日志:

Colour at 0 is (null) 
Colour at 1 is (null)
....
didReceiveResponse
connectionDidFinishLoading
Succeeded! Received 287 bytes of data
getting colour for data : red 

因此它首先将图像分配给单元格,然后执行请求.当然,我总是在else子句中看到该图像.

so it firsts assigns the image to cell and then does the request. And of course I always see the image in the else clause.

我想先进行数据提取,然后分配图像.怎么做? 因为这是我的第一个ios应用,请完成答案或给我示例代码,或提供指向教程的链接.

I want to do first the data fetching and then assign images. How to do it? Because it is my first ios app please either complete the answer or give me sample code or give a link to a tutorial.

好吧,我修复了错字错误.现在,当我尝试重新加载时,我得到一个EXC_BAD_ACCESS错误.如果我取消注释重载数据,则不会显示此错误. 看到我编辑的代码.我的表视图已连接"到IBOutlet mainMap.

OK i fixed that typo error. And now I get a EXC_BAD_ACCESS error when I am trying to reload. if i uncomment the reload data this error does not show up. See my edited code. My table view is "connected" to IBOutlet mainMap.

错误出现在此行:

 NSLog(@"Colour at %d is %@ ",indexPath.row,[colour objectAtIndex:indexPath.row]);

第二次(我是指由于重新加载数据而调用cellForRowAtIndexPath时)

at the second time (I mean when cellForRowAtIndexPath is called because of the reload Data)

错误是:EXC_BAD_ACCESS(代码1). 也许数组被释放或类似的东西?我应该检查什么?

error is: EXC_BAD_ACCESS (code 1). Maybe array is released or something like this? What should I check for that?

推荐答案

做所有事情,但是在初始化之后隐藏tableView:

Do every thing as you do it, but hide the tableView after its initialization:

tableView.alpha = 0;

然后在填充数组后在connectionDidFinishLoading:方法中,首先用[tableView reloadData];(再次调用cellForRowAtIndexPath)重新加载tableView,然后用tableView.alpha = 1;显示tableView.

Then in your connectionDidFinishLoading: method after filling your array, first reload the tableView with [tableView reloadData]; (that is calling cellForRowAtIndexPath again) and then show your tableView with tableView.alpha = 1;.

您还可以使用动画淡入和淡出tableView,但这是另一个问题.

You can also fade the tableView in and out with a animation, but that's another question.

如果您想知道如何编写这样的JSON-Parsing-tableView-filling应用程序,请访问以下链接:
http://mobile.tutsplus.com/tutorials/iphone/iphone- json-twitter-api/

If you want to know, how to write such a JSON-Parsing-tableView-filling App visit this link:
http://mobile.tutsplus.com/tutorials/iphone/iphone-json-twitter-api/

顺便说一句:

这是什么:colours== [NSMutableArray array]; ???它必须像这样:

WHAT IS THIS: colours== [NSMutableArray array];??? It has to be like THIS:

colours = [NSMutableArray array];. 

请注意单等号

这篇关于获取json对象,然后分配给uitable视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆