【问题标题】:How to add activity indicator to the image in UITableView?如何在 UITableView 中为图像添加活动指示器?
【发布时间】:2013-03-11 04:10:42
【问题描述】:

我创建了自定义tableView 并在其他类中使用自定义tableView 类来显示tableView..

我正在从服务器加载图像并将其显示在tableViewRow.. 每个image 中都不同..

在屏幕上,10 张图像中只有 7 张会出现,当我向下滚动时,前 3 张图像会重复一段时间,当这些图像加载时,它会显示正确的图像.. 但最初直到新图像出现,它才会显示旧图像图像,我想放置一个活动指示器来显示图像正在加载而不是旧图像..

我想在image 的位置添加activity Indicator,直到image 加载..

我的代码是...

self.tableViewX = tableView;
static NSString *simpleTableIdentifier = @"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView1 dequeueReusableCellWithIdentifier:simpleTableIdentifier];
 if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
    {
NSString *imageURL = [NSString stringWithFormat: @"www.xyz.image.png"];
cell.thumbnailImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];
 });

请帮助我提供一些示例代码..

【问题讨论】:

  • 您可以使用异步图像视图来实现这一点。因为图像显示是这样的,因为您使用的是 dequeueReusableCellWithIdentifier 所以制作一个不同的标识符或使单元格为零..然后你可以实现它跨度>
  • @08442 : 如何使用异步图像视图?你有任何我可以理解的示例代码吗..
  • @bhargavi:我们需要 ASIHTTPRequest 类来执行此操作吗?
  • 我不能创建图像的子视图,以便活动指示器在图像加载之前一直工作吗?

标签: ios objective-c objective-c-blocks uiactivityindicatorview


【解决方案1】:

您可以使用“AsyncImageView”类文件,它会同步加载图像并在图像加载时显示活动指示器

您可以从以下链接下载“AsyncImageView”类文件:- https://www.dropbox.com/s/peazwjvky9fsjd7/Archive.zip

在.m文件中导入AsyncImageView类

 #import "AsyncImageView.h" 

在 indexpath 方法的 tableview 单元格中

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *simpleTableIdentifier = @"SimpleTableCell";
     SimpleTableCell *cell = (SimpleTableCell *)[tableView1 dequeueReusableCellWithIdentifier:simpleTableIdentifier];
     if (cell == nil)
     {
           NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
      cell = [nib objectAtIndex:0];
     }
     NSString *imageURL = [NSString stringWithFormat: @"www.xyz.image.png"];
     AsyncImageView *async = [[AsyncImageView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
     [async loadImageFromURL:[NSURL URLWithString:imageURL]];
     [cell.thumbnailImageView addSubview:async];
}

试试这个你的问题会解决的。

【讨论】:

  • 这将是什么 "[async loadImageFromURL:[NSURL URLWithString:strThumbImageUrl] imageName:@""];"声明会做什么?什么是 strThumbImageUrl?和imageURL一样吗?
  • 这是您的图片网址,请检查答案,我已编辑答案
  • 它显示错误“'AsyncImageView' 没有可见的@interface 声明选择器'loadImageFromURL:imageName:'”
  • 这是 asyncImageView 类文件方法删除 imagename:@"" 并尝试看看我已经编辑了代码
  • 我也试过 "[async loadImageWithURL:[NSURL URLWithString:imageURL]]" 但它仍然显示 "No visible @interface for 'AsyncImageView' 声明选择器 'loadImageWithURL:'" 错误..跨度>
猜你喜欢
  • 2011-12-18
  • 1970-01-01
  • 1970-01-01
  • 2015-06-18
  • 2020-05-19
  • 1970-01-01
  • 2014-01-08
  • 1970-01-01
  • 2016-04-19
相关资源
最近更新 更多