想要在UITableViewCell上设置图像时在UITableViewCell上显示UIActivityIndi​​cator

时间:2014-02-18 10:59:50

标签: iphone objective-c uitableview ios7

我在UITableView中有一个水平和垂直滚动的表格,每个单元格都有一个UIImageView,我通过URL在单元格的UIImageView上设置图像。我想在所有单元格上显示ActivityIndi​​cator,直到下载图像。我有搜索很多,但没有找到任何好的。 这是我的CellForRowAtIndexPath

代码
      static NSString *CellIdentifier = @"ArticleCell";
    NSDictionary *dict=[self.articles objectAtIndex:indexPath.row];
    __block ArticleCell_iPhone *cell = (ArticleCell_iPhone *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    //added for uiacticvaty indicator on uitableviewcell

    UIActivityIndicatorView *loadingActivity = [[UIActivityIndicatorView alloc]
                                                initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    [loadingActivity startAnimating];

    loadingActivity.frame = cell.thumbnail.frame;
    loadingActivity.frame = CGRectMake(0, 0, 90  , 90);

    [loadingActivity setBackgroundColor:[UIColor greenColor]];
    UIView* parent = [cell.thumbnail superview];
    [parent addSubview:loadingActivity];
    [parent bringSubviewToFront:loadingActivity];
//
    //added for uiacticvaty indicator
    if (cell == nil) 
    {
        cell=[[[ArticleCell_iPhone alloc]initWithFrame:CGRectMake(0, 0, kCellWidth_iPad, kCellHeight_iPad) withNSDict:dict] autorelease];
        __block NSDictionary *currentArticle = [self.articles objectAtIndex:indexPath.row];

        dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

        dispatch_async(concurrentQueue, ^{
            UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[currentArticle objectForKey:@"thumbnail_url"]]]];

            dispatch_async(dispatch_get_main_queue(), ^{
                [cell.thumbnail setImage:image];
//                [loadingActivity stopAnimating];

                cell.titleLabel.text = [currentArticle objectForKey:@"Title"];


            });
        }); 

    }



//    [cell.imagefooter setImage:[UIImage imageNamed:@"line"]];

    return cell;
}

3 个答案:

答案 0 :(得分:0)

考虑利用SDWebImage等开源解决方案(UIActivityIndicator-for-SDWebImage)。

答案 1 :(得分:0)

有必要为cell.imageView提供一个“背景”......但必须是一个图像,而不仅仅是一个框架。所以只需创建一个UIImage作为“白色背景”并在cell.imageView.image中设置。代码将是:

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] 
    initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
UIImage *whiteback = [[UIImage alloc] initWithContentsOfFile:@"whiteback.png"];
 cell.imageView.image = whiteback;
[cell.imageView addSubview:spinner];
[spinner startAnimating];
cell.textLabel.text=@"Carregando...";
[whiteback release];
[spinner release];

whiteback.png只是一个25x25像素的白色方块...... 感谢

答案 2 :(得分:0)

使用自定义单元格为您的活动指示器添加标记。

  UITableViewCell *aCell=[tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

    UIActivityIndicatorView *aActivity=(UIActivityIndicatorView *)[aCell viewWithTag:1];
    [aActivity startAnimating];
    return aCell;

我在我的演示中尝试了这个代码并且它的工作非常完美。

相关问题