如何从JSON更新TableView

时间:2013-06-27 11:06:47

标签: ios json nsmutablearray tableview

从JSON更新我的webdata我遇到了很大的问题。所以我有简单的TableView和来自JSON URL的解析。当我按下我的按钮 - 从JSON的信息写入tableviewCell但我需要这个信息将随时更新。所以我有我的代码:

-(void)viewDidAppear:(BOOL)animated {

        [super viewDidAppear:animated];

        NSURL *url = [NSURL URLWithString:@"http://url.json"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];

        connection = [NSURLConnection connectionWithRequest:request delegate:self];
        if (connection) {
            webdata = [[NSMutableData alloc]init];

            [mytableview reloadData];

        }

我写了这段代码,但我的信息没有更新,我做错了什么?我还尝试了ViewDidAppear,viewWillAppear。但没什么。

添加了cellForRow方法。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{



    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(! cell)
    {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
    }



    cell.textLabel.text = [array objectAtIndex:indexPath.row];

3 个答案:

答案 0 :(得分:3)

尝试使用基于块的NSURLConnection。我猜你形成webdata的方式就是问题。

NSURL *url = [NSURL URLWithString:@"http://url.json"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:urlRequest
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *urlResponse, NSData * webdata, NSError *reponseError) {

    NSArray *json = [NSJSONSerialization JSONObjectWithData: webdata
                                                    options:0
                                                      error:nil];
    for (NSDictionary *person in json) {
        //Are these numbers or strings
        NSString *art = person[@"artist"];
        NSString *song2 = person[@"start"];
        [array addObject:art];
        [array2 addObject:song2];
    }
    [mytableview reloadData];

}];

不建议保留两个数组以仅显示艺术家姓名和歌曲名称。数据形成良好。将这些数据保存在数组中。在tableView:cellForRowAtIndexPath:中,当您需要使用索引显示数据时,获取该人并找到相应的名称和歌曲名称。

答案 1 :(得分:1)

使用此-(void)connectionDidFinishLoading:(NSURLConnection *)connection NSURLConnection delgate method.

  connection.delgate=self;
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// write your parsing logic 
// reload your table here
[mytableview reloadData];

}

答案 2 :(得分:0)

JSON引入数据是一个异步过程。因此,当将对象放置到数组时,您需要在行后面放置NSNotification。并创建一个自定义功能,当您的reload table NSNotificationCentre array has all the data from JSON以及此功能时, -your JSON block{ [[NSNotificationCenter defaultCenter]postNotificationName:@"newList" object:nil]; } - (void)viewDidLoad { [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reloadData) name:@"Doreload" object:nil]; } -(void)reloadData // Method to reload data and show data in table view { [self.table reloadData]; } 会调用{{1}} }。

如果您需要任何进一步的帮助,请与我们联系。

修改

{{1}}