1

JSON から Web データを更新する際に大きな問題があります。SO 私は、JSON URL からの解析を使用した単純な TableView を持っています。ボタンを押すと、JSON からの情報が tableviewCell に書き込まれますが、この情報はいつでも更新される必要があります。SO私は私のコードを持っています:

-(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];
4

3 に答える 3

3

ブロックベースの NSURLConnection を試してください。Webデータを形成する方法が問題だったと思います。

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];

}];

アーティスト名と曲名だけを表示するために 2 つの配列を保持することはお勧めしません。データは適切に形成されています。これらのデータをそのまま配列に保存します。インデックスを使用してデータを表示する必要があるtableView:cellForRowAtIndexPath:場合は、人物を取得し、対応する名前と曲名を見つけます。

于 2013-06-27T11:33:46.537 に答える
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];

}
于 2013-06-27T11:11:58.970 に答える