How To Reload data from URL ???Only Can Load data

2019-08-11 02:31发布

问题:

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

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;

}

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

So I add

-(void)viewDidLoad{

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

}

and change the get URLrequest from (void)viewDidLoad to

- (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!!!!");
}

HERE IS THE PROBLEM ~

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

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

What's Wrong When My program Reload Data pass to cell failed??

回答1:

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