非常に奇妙な動作に気付きました。他の誰かがこれに遭遇したかどうかを確認したいと思っています。非同期 API 呼び出しを行っています (以下のコード)。呼び出しが完了すると、呼び出しの結果から配列が取り込まれ、テーブルがリロードされます (cellForRowAtIndexPath
呼び出される必要があります)。これにより、テーブル ビューが配列のデータで更新されます。ただし、テーブルビューのデータは、他の手段からリロードする必要があるまで表示されません。たとえば、タブをクリックしてビューを変更し、元のビューに戻る場合などです。「テーブルの更新」には欠けている側面があるようですがreloadData
、非同期呼び出しが戻ったときに呼び出しています。
コード:
-(void)refreshWeeksOffers
{
[array removeAllObjects];
NSMutableURLRequest *request =
[WebRequests createPostRequestWithApiCall:@"getResults" bodyData:@"params={\"locale\" : \"US\"}"];
[NSURLConnection
sendAsynchronousRequest:request
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error)
{
if ([data length] >0 && error == nil)
{
// parse home page offers from resulting json
JsonParser *parser = [[JsonParser alloc] initWithData:data];
array = [parser parseHomepageResults];
[self.topWeekTable reloadData];
}
else if ([data length] == 0 && error == nil)
{
NSLog(@"Nothing was downloaded.");
}
else if (error != nil){
NSLog(@"Error = %@", error);
}
}];
[self.topWeekTable reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
Offer *currentOffer = (Offer *)[array objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%.1f%% Back", currentOffer.advertisedRate];
NSData *data = [NSData dataWithContentsOfURL:currentOffer.storeImage];
UIImage *img = [[UIImage alloc] initWithData:data];
cell.imageView.image = img;
return cell;
}