iphone - 通过 NSURLConnection 异步加载 TableView 图像

标签 iphone objective-c ios

正如标题所暗示的,我正在尝试通过 NSURLConnection 异步加载 tableview 图像。这里的问题是,当我向下滚动时,我的表格 View 往往会在表格 View 中加载多个国家/地区的相同国旗图像,然后在滚动时为多个国家/地区加载不同的国旗图像(因为多个国家/地区具有相同的国旗图像)备份。我注意到,只有当我非常缓慢地滚动时,我才会得到不同国家的不同旗帜图像,但图像不正确。我不太确定目前的问题是什么。这是我的代码:

#import "ViewController.h"

@implementation ViewController
@synthesize tableView;
@synthesize countryNamesArray;
@synthesize receivedData; 
@synthesize flagImage;
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{   
    countryNamesArray=[[NSArray alloc] initWithObjects:@"India",@"USA",@"Antarctica",@"Brazil",@"Canada",@"China",@"France",@"Germany",@"Italy",@"Japan",@"Kenya",@"Malaysia",@"Mexico",@"South Africa",@"United Kingdom",@"Vietnam",nil];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

-(NSInteger)tableView:(UITableView *)tableview numberOfRowsInSection:(NSInteger)section{
    return 16;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // This method is called when the server has determined that it
    // has enough information to create the NSURLResponse.

    // It can be called multiple times, for example in the case of a
    // redirect, so each time we reset the data.

    // receivedData is an instance variable declared elsewhere.
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the new data to receivedData.
    // receivedData is an instance variable declared elsewhere.
    [receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{
    // release the connection, and the data object
    // receivedData is declared as a method instance elsewhere


    // inform the user
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // do something with the data
    // receivedData is declared as a method instance elsewhere
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
    flagImage = [UIImage imageWithData: receivedData];
}

-(void)issueRequest:(NSString *)fullCountryImageURL{
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:fullCountryImageURL]
                                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                                          timeoutInterval:60.0];
    // create the connection with the request
    // and start loading the data
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (theConnection) {
        // Create the NSMutableData to hold the received data.
        // receivedData is an instance variable declared elsewhere.
        receivedData = [NSMutableData data];


    } 
    else {
        // Inform the user that the connection failed.
    }
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"countryCell";
    UITableViewCell *cell=(UITableViewCell *) [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell==nil){
        cell=[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    // Create the request.


    NSString *flagImageURLPartOne = @"http://www.flagimage.com/";
    NSString *countryNumber = [NSString stringWithFormat:@"%i", indexPath.row+1];
    NSString *flagImageURLPartTwo = @".png";
    NSString *fullCountryImageURL = [NSString stringWithFormat:@"%@%@%@", flagImageURLPartOne, countryNumber, flagImageURLPartTwo];

    [self issueRequest:fullCountryImageURL];

    cell.imageView.image = flagImage;
    cell.textLabel.text= [countryNamesArray objectAtIndex:indexPath.row];

    return cell;
}

-(void) tableView:(UITableView *) tableview didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}


@end

有人有什么想法吗?

最佳答案

不知何故,您必须通过数据结构将flagImage与国家/地区名称关联起来。

看一下 tableView:cellForRowAtIndexPath: 中发生的情况,您异步触发图像请求,然后将当前 flagImage 分配给单元格的 ImageView 。但是 NSURLConnection 委托(delegate)方法会在未来某个不确定的时间触发,并在那时重新分配 flagImage

您当前在 tableView:cellForRowAtIndexPath: 中所做的操作适用于同步 NSURLConnection,但您会被异步委托(delegate)回调绊倒。

关于iphone - 通过 NSURLConnection 异步加载 TableView 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12787681/

相关文章:

iphone - 在 UITableView 的顶部和底部添加按钮

ios - 为什么在遍历 firebase 数据库时得到 nil?

iphone - 如何从 callOutAccessoryControlTapped : method 将数据发送到详细 vc

iphone - 获取 UIImagePickerController 选择的视频尺寸

Objective-C iOS,为文本更新设置延迟

ios - 'application :handleActionWithIdentifier:forRemoteNotification:completionHandler 实现中的参数类型冲突

iphone - 在表格 View 中选择后如何将数据传递到详细 View ?

iphone - 如何调整 UIToolBar 左右内边距

ios - 如何使用 SwiftyJSON 显示 API 图像

ios - UI 挂起,直到完成所有提取