【问题标题】:Images display on UITableViewCell图像显示在 UITableViewCell 上
【发布时间】:2016-02-05 23:46:10
【问题描述】:

我正在尝试在 UITableViewCell 中显示图像,我正在使用 Web 服务来获取图像名称。这是我在网络服务的响应中得到的。

"2514466130834f61a9g38d57h0e2cb-25-ProfilePic.PNG",
"26144482598634297156heb8c0dafg-26-ProfilePic.PNG",
"2714464392629h60253gd4ec8b1f7a-27-ProfilePic.PNG",
"2814449113316a7gh5db0e2139c84f-28-ProfilePic.PNG",
"2914454043856e3519ah0dbf27cg84-29-ProfilePic.PNG"

以上响应以数组格式保存。然后将这些数组对象附加到一个 url 上,我需要在 UIImageView 上显示图像。 请帮帮我。

【问题讨论】:

  • 您有一个返回图像文件名列表的网络服务?您想下载这些图像并将它们显示在表格中吗?这个任务有很多部分。你已经完成了哪些,还有哪些问题?
  • 您是否使用默认单元格的自定义单元格?并请分享代码,以便我可以更好地帮助您...

标签: ios objective-c xcode uiimageview


【解决方案1】:

准备您的网址,如下所示。

// Consider this is your prefix url 
NSString *urlPrefix = @"https://your_url_prefix/"; // like http://stackoverflow.com/

// Consider this is the array you parsed from the web service
NSArray *resourceArray = @[@"2514466130834f61a9g38d57h0e2cb-25-ProfilePic.PNG", @"26144482598634297156heb8c0dafg-26-ProfilePic.PNG", @"2714464392629h60253gd4ec8b1f7a-27-ProfilePic.PNG", @"2814449113316a7gh5db0e2139c84f-28-ProfilePic.PNG", @"2914454043856e3519ah0dbf27cg84-29-ProfilePic.PNG"];

在你的tableView的数据源方法中准备资源url:cellForRowAtIndexPath

NSString *resourceURL = [urlPrefix stringByAppendingString:[resourceArray objectAtIndex:indexPath.row]];

并使用SDWebImage 库加载图像并将其设置到您的单元格imageView

[cell.imageView setImageWithURL:resourceURL];

注意:SDWebImage lib 可以从SDWebImage Git 下载 在你的项目中添加这个库文件并像下面这样导入使用。

#import "UIImageView+WebCache.h"

【讨论】:

    【解决方案2】:

    首先下载异步类Here

    然后把你的 .m 文件放进去

    #import "AsyncImageView.h"

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *simpletableidentifier=@"simpletableidentifier";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpletableidentifier];
        if(cell == nil)
        {
            cell.backgroundColor=[UIColor clearColor];
            cell = [[UITableViewCell alloc ]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpletableidentifier];
        } 
    
       AsyncImageView *Galleryimg = [[AsyncImageView alloc]initWithFrame:CGRectMake(0,0,40,40)];
       Galleryimg.contentMode = UIViewContentModeScaleAspectFill;
       Galleryimg.clipsToBounds=YES;
       Galleryimg.tag=i;
       Galleryimg.imageURL=[NSURL URLWithString:[Yourstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
       [scroll addSubview:Galleryimg];
       return cell;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-06
      • 1970-01-01
      • 1970-01-01
      • 2011-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-23
      相关资源
      最近更新 更多