2012-06-26 41 views
0

在我的應用程序中,我通過php文件在互聯網和數據顯示在表視圖中的數據庫git dat,但我需要每分鐘重新加載我的數據,我可以每分鐘獲得新的數據,但我不能取代它在表格視圖中。從數據庫重新載入TableView數據

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 
+0

關於Q1的確定anwerd – user183790

+0

Q2是否存在 – user183790

回答

1

要強制表重新加載其信息,從您的視圖控制器類調用[self.tableView reloadData]。這將再次調用所有的數據源方法,並且它也會更新顯示。

+0

謝謝while(1 == 1){print(Vere); }對於快速回答很有幫助 – user183790

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]; 
} 
+0

執行此操作並添加一個NSTimer,以便每分鐘執行一次此方法。同樣遵循@ rishi的建議,並在這種方法中檢查數據的變化,而不是單純地下載數據。也可以使用網絡活動指示器。 – erran

1

您需要使用reloadData方法重新加載表視圖,以顯示新的數據。

但是當你mentioned-「但我需要每分鐘刷新我的數據」

所以對於這一點,你可以檢查與現有數據的數據收到,如果事情在新的數據被改變,那麼只能重裝表,否則跳過那個。

如果您每隔一分鐘就會重新加載一次表格,而沒有更改數據,那將是不正確的,這也會降低您的應用程序的速度。

相關問題