ios - 如何在 UITableView 中的图像中添加事件指示器?

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

我已经创建了自定义 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]]];
 });

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

最佳答案

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

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

在.m文件中导入AsyncImageView类

 #import "AsyncImageView.h" 

在您的 tableview 单元格中的 indexpath 方法

-(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];
}

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

关于ios - 如何在 UITableView 中的图像中添加事件指示器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15331079/

相关文章:

iphone - 比较 switch 语句中的 ALAssetGroupType

ios - 如何允许用户输入多行文本 Swift(如 Facebook 或 Twitter 帖子)

ios - 去 Segue 还是去 didSelectRowAtIndexPath?

objective-c - 使用游戏中心发送和接收字符串

objective-c - 在 Cocoa 中创建动画 GIF - 定义帧类型

objective-c - 如何知道 NSURLSessionDataTask 的 for 循环何时完成

ios - 在 iPhone 上远程打开相机

ios - 我如何知道无法将 iPhone 应用程序提交到应用程序商店的原因?

android - IOS Swift 中的应用类

Swift 中的 Objective-C block - 变量丢失?