Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c iOS中UITableView异步映像加载的建议_Objective C_Ios_Ios5_Asynchronous_Afnetworking - Fatal编程技术网

Objective c iOS中UITableView异步映像加载的建议

Objective c iOS中UITableView异步映像加载的建议,objective-c,ios,ios5,asynchronous,afnetworking,Objective C,Ios,Ios5,Asynchronous,Afnetworking,我当前正在使用以下代码加载带有左侧缩略图、标题和副标题的表格: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIde

我当前正在使用以下代码加载带有左侧缩略图、标题和副标题的表格:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    NSDictionary *post           = [posts objectAtIndex:indexPath.row];

    cell.textLabel.text       = [post objectForKey:@"post_text"];
    cell.detailTextLabel.text = [post objectForKey:@"post_author_name"];

    NSString *postpictureUrl = [post objectForKey:@"picture"];
    NSData   *data           = [NSData dataWithContentsOfURL:[NSURL URLWithString:postpictureUrl]];

    cell.imageView.image = [UIImage imageWithData:data];

    return cell;
}
当然,由于我在这里进行的同步加载,这在生产环境中不起作用

对于在这种情况下异步加载图像,您有什么建议


我已经找到了(还没有使用过),但不知道是否有更轻量级的方法来解决这个问题。

AsyncImageView
是您的朋友,请查看


只需设置
imageURL
,您就可以开始了。

这可能会有所帮助。它非常深入,使用线程加载远程图像。对于这种情况,延迟加载是最好的方法@巴拉:我看到了那个,但我认为它充满了ARC之前的代码(虽然它说它是iOS 5+),我认为,使用ARC制作它非常简单,。试一试虽然这个链接可以回答问题,但最好在这里包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,仅链接的答案可能会无效。如果您仍然遇到延迟加载问题,请告诉我。@user1573162-代码似乎无法编译,因为缺少闭包-此外,我尝试了另一个版本,但图像没有显示-您想对上面的建议进行任何更改吗?谢谢!thx@user1573162,没有错误,但它不会在左边显示图像(标题和副标题)加载OK——顺便说一句,
[cell setNeedsLayout]行需要修改removed@user1573162-好的,现在我注意到,当我在表格单元格上单击tal时,图像出现在左侧-为什么会出现这种情况?是否需要一个占位符?
Use lazy loading in table view
Use this code


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

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {

            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }

        NSDictionary *post = [posts objectAtIndex:indexPath.row];

        cell.textLabel.text = [post objectForKey:@"post_text"];
        cell.detailTextLabel.text = [post objectForKey:@"post_author_name"];
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,  0ul);
        dispatch_async(queue, ^{
         //This is what you will load lazily
      NSString *postpictureUrl = [post objectForKey:@"picture"];
        NSData   *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:postpictureUrl]];
            dispatch_sync(dispatch_get_main_queue(), ^{

        cell.imageView.image = [UIImage imageWithData:data];
                [cell setNeedsLayout];
      }); 
}); 

        return cell;
    }