如何从URL重新加载数据???只能在viewdidload中加载数据?

时间:2010-09-16 09:28:39

标签: iphone url sdk cell reloaddata

好的,首先这个程序可以通过此代码从URL加载plist,我把它放在

- (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文件初始化为tableview单元格

- (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数据以启动它

所以我添加

-(void)viewDidLoad{

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

}

并将获取网址请求从(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

获取任何plist数据

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

当我的程序重新加载数据传递给单元格时出错了什么?

1 个答案:

答案 0 :(得分:1)

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