Как перезагрузить данные с URL? Загружать данные можно только в viewdidload?

ОК,Сначала эта программа может загрузить plist из URL-адреса по этому коду,и я поместил его в

- (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 для инициализации ячейки таблицы

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

}

и измените URL-запрос на получение с (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 init, похоже, не получила никаких данных plist из timeFired

Я проверяю результат консоли, я вижу, что данные plist возвращаются каждые 3 секунды (определить REFRESH_STATUS_TIME = 3.0;)

Что не так, когда моя программа перезагружает передачу данных в ячейку со сбоем??


person Webber Lai    schedule 16.09.2010    source источник
comment
О, я установил точку останова в - (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath {...} НЕ ОСТАНОВИЛСЯ ЗДЕСЬ   -  person Webber Lai    schedule 16.09.2010


Ответы (1)


Я не видел ни одной строки с [self.tableView reloadData];. Вы должны вызвать это, чтобы перезагрузить данные табличного представления.

person vodkhang    schedule 16.09.2010
comment
Вызывает ли он ваш - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {...} ? - person vodkhang; 16.09.2010
comment
НЕТ... Я думаю, что это не вызывает эту функцию, потому что это не остановка на следующей строке под точкой останова - person Webber Lai; 16.09.2010
comment
также это не вызов - (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) section { return [count self.plist]; } - person Webber Lai; 16.09.2010
comment
Если вы или страна опаздываете, мы можем попытаться решить этот вопрос завтра .... извините за это - person Webber Lai; 16.09.2010
comment
вы уверены, что он вызывает ваш [self.tableView reloadData]; - person vodkhang; 16.09.2010