0

我有一个 uitableview,每个单元格都有一个来自 facebook 图像配置文件和 2 个 uilabels 的图像(来自单例,不是互联网请求)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    static NSString *CellIdentifier = @"MenuCell";
    MenuCellViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[MenuCellViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    //Singleton
    DataModel *m = [DataModel getModel];
    switch ([indexPath section]) {
        case 0: //Only Header
            break;

        case 1: //My cells
        {
            //If footer - set footer background
            if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1){
               [cell setBackgroundView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"imgFooter.png"]]];
            //Else - set normal cell background
            } else {
                [cell setBackgroundView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"img.png"]]];
            }

            //Get Facebook image
            strForImg = [[NSString alloc] initWithFormat:@"https://graph.facebook.com/%@/picture?type=square",[[[m list] objectAtIndex:indexPath.row] objectForKey:@"id"]];    
            url =[NSURL URLWithString:strForImg];
            img = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
            [cell.imgProfile setImage:img];

            //Set some text (uilabels)
            cell.lblNom.text = [[[m list] objectAtIndex:indexPath.row] objectForKey:@"name"];
            cell.lblTour.text = [[[m list] objectAtIndex:indexPath.row] objectForKey:@"description"];
            break;
        }
    }
}

这段代码非常慢,并且在从 facebook 动态加载每个单元格的配置文件图像时冻结了 tableview。

我尝试使用这样的调度

case 1://My cells
    {
        if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1){
           [cell setBackgroundView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"imgFooter.png"]]];
        } else {
            [cell setBackgroundView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"img.png"]]];
        }

        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
        dispatch_async(queue, ^{
            dispatch_sync(dispatch_get_main_queue(), ^{
                strForImg = [[NSString alloc] initWithFormat:@"https://graph.facebook.com/%@/picture?type=square",[[list objectAtIndex:indexPath.row] objectForKey:@"id"]];

                url =[NSURL URLWithString:strForImg];
                img = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
                [cell.imgProfile setImage:img];

            });
        });
        cell.lblNom.text = [[[m list] objectAtIndex:indexPath.row] objectForKey:@"name"];
        cell.lblTour.text = [[[m list] objectAtIndex:indexPath.row] objectForKey:@"description"];
        break;
    }

速度略有提高,但滚动时仍然冻结。

我的派送不正确吗?(我刚刚发现,可能没有很好用)。

或者有没有更好的方法?我喜欢 facebook 的朋友选择器。它会放置一个默认的配置文件图像,并在从 Internet 接收到它时刷新正确的图像配置文件。这个魔法是怎么做到的?:)

4

1 回答 1

1

修复此块:

 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
        dispatch_async(queue, ^{
                strForImg = [[NSString alloc] initWithFormat:@"https://graph.facebook.com/%@/picture?type=square",[[list objectAtIndex:indexPath.row] objectForKey:@"id"]];

                url =[NSURL URLWithString:strForImg];
                img = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
            dispatch_sync(dispatch_get_main_queue(), ^{
                [cell.imgProfile setImage:img];

            });
        });
于 2012-12-08T23:54:48.507 回答