如何从URL重新加载数据?只能在viewdidload中加载数据吗? [英] How To Reload data from URL ???Only Can Load data in viewdidload?

查看:93
本文介绍了如何从URL重新加载数据?只能在viewdidload中加载数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,首先该程序可以通过此代码从URL加载plist,然后将其放入

OK,First this program can load plist from URL by this code,and I put this in

- (void)viewdidLoad{
   NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://someurl.php"]
             cachePolicy:NSURLRequestUseProtocolCachePolicy
            timeoutInterval:60.0];

 NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
 NSLog(@"\n\nCONNECTION:   %@", theConnection);
 NSData *returnData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil]; 
 NSString *listFile = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];   
 self.plist = [listFile propertyList];
}

比我用plist文件初始化表格单元格

than I take the plist file to init the tableview cell

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

 static NSString *CellIdentifier = @"cell";

 LightCell0 *cell =(LightCell0 *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
 if (cell == nil) {
  cell = [[[LightCell0 alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
 }
 // Set up the cell…
 [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

  cell.lightLocation.text =  [[[self.plist objectAtIndex:indexPath.row] valueForKey: @"nodeName"]description];


       return cell;

}

现在我需要继续重新加载URL数据以进行初始化

now I need to keep reloading the URL data to init it

所以我添加

-(void)viewDidLoad{

timer = [NSTimer scheduledTimerWithTimeInterval:REFRESH_STATUS_TIME
                target:self
                 selector:@selector(timerFired:)
                 userInfo:nil
               repeats:YES];

}

并从更改get URLrequest (void)viewDidLoad

- (void)timerFired:(NSTimer *)theTimer{
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.someurl.php"]
             cachePolicy:NSURLRequestUseProtocolCachePolicy
            timeoutInterval:60.0];

 NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
 NSLog(@"\n\nCONNECTION:   %@", theConnection);
 NSData *returnData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil]; 
 NSString *listFile = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];   
 self.plist = [listFile propertyList];
 NSLog(@"Timefired!!!!");
}

这里是问题〜

TableView单元格初始化似乎没有从 timeFired

The TableView cell init seems didn't get any plist data from the timeFired

我检查控制台结果,我看到有一个plist数据每3秒返回一次
(定义REFRESH_STATUS_TIME = 3.0;)

I check the console result,I can see there is a plist data get back every 3 sec (define REFRESH_STATUS_TIME = 3.0;)

当我的程序重新加载数据失败时怎么了?

推荐答案

我没看到 [self.tableView reloadData]; 行。您必须调用它来重新加载表格视图数据

I didn't see any lines with [self.tableView reloadData];. You have to call this to reload your table view data

这篇关于如何从URL重新加载数据?只能在viewdidload中加载数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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