0

私のアプリでは、インターネットのphpファイルとテーブルビューに表示されるデータによってDBからgit datを実行しますが、毎分データをリロードする必要があります.毎分新しいデータを取得できますが、テーブルビューでそれを置き換えることはできません.

1) やり方を知っていますか?

2)私のコードをよりシンプルにする方法を知っていて、その不要なコードを削除する方法を知っているなら? 私はあなたに感謝します。

私のViewController.m

#import "ViewController.h"
#import "CJSONDeserializer.h"

@implementation ViewController
@synthesize tblMain, rows;

NSURL *url;
NSString *jsonreturn;
NSData *jsonData;
NSError *error;
NSDictionary *dict;
UITableViewCell *cell;
static NSString *CellIdentifier;

- (void)viewDidLoad{
    [super viewDidLoad];

    [self GetData];
}

-(void)GetData{
    url = [NSURL URLWithString:@"http://ar2.co/savola/"];
    jsonreturn = [[NSString alloc] initWithContentsOfURL:url];
    NSLog(jsonreturn);
    jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
    dict = [[[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error] retain];
    rows = [dict objectForKey:@"savola"];
    NSLog(@"Array: %@",rows);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [rows count];
}

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

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    dict = [rows objectAtIndex: indexPath.row];

    cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@", [dict objectForKey:@"company"], [dict objectForKey:@"price"]];
    cell.detailTextLabel.text = [dict objectForKey:@"time"];

    return cell;
}


@end
4

4 に答える 4

1

reloadData新しいデータを表示するには、メソッドを使用してテーブルビューを再読み込みする必要があります。

しかし、あなたが言ったように-「しかし、私は毎分私のデータをリロードする必要があります」

したがって、このために、受信したデータを既存のデータで確認できます。新しいデータで何かが変更された場合は、テーブルのみをリロードします。それ以外の場合はスキップします。

データを変更せずに1分ごとにテーブルをリロードすると、それは正しくなく、アプリの速度も低下します。

于 2012-06-26T06:41:54.553 に答える
1

テーブルの情報を強制的にリロードするには[self.tableView reloadData]、View Controller クラスから呼び出します。これにより、すべてのデータ ソース メソッドが再度呼び出され、表示も更新されます。

于 2012-06-26T06:36:04.750 に答える
1

サーバーから新しいデータをフェッチした後、テーブルをリロードする必要があります。GetData メソッドのように:

- (void)GetData
{
    url = [NSURL URLWithString:@"http://ar2.co/savola/"];
    jsonreturn = [[NSString alloc] initWithContentsOfURL:url];
    NSLog(jsonreturn);
    jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
    dict = [[[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error] retain];
    rows = [dict objectForKey:@"savola"];
    NSLog(@"Array: %@",rows);

   [yourTable reloadData];
}
于 2012-06-26T06:37:48.123 に答える
0

このような状況では、NSFetchedResultsControllerの使用を検討することをお勧めします。必要な場合にのみ、テーブルが自動的に更新されます。

于 2012-06-26T06:55:08.473 に答える