从表视图中的URL异步获取图像

Noo*_*ass 2 uitableview nsurlconnection ios

所以我正在开发一个应用程序,其中包含一个包含单元格中图像的表视图.我从URL下载所有这些图像并将它们存储在一个数组中.我需要异步加载这些图像,所以我决定使用NSURLConnectionDataDelegate.我试过这段代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSUInteger row = [indexPath row];
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[URLsArray objectAtIndex:row]]];
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (theConnection) {
        receicedData = [[NSMutableData alloc] init];
    } else {
        NSLog(@"Failed.");
    }
    return cell;
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用.我不知道该写些什么:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
Run Code Online (Sandbox Code Playgroud)

将图像设置为单元格中的imageViews.有人可以帮忙吗?

Rya*_*sal 6

我强烈建议使用,AFNetworking因为它会解决这些问题.

至少,看看他们的UIImageView类别,因为它允许你这样做:

UIImageView *imgView = //alloc, get it from a nib, whatever;
[imgView setImageWithURL:[NSURL URLWithString:@"StringPath"]];
Run Code Online (Sandbox Code Playgroud)