【问题标题】:objective-c image not appearing in custom UITableViewCellobjective-c 图像未出现在自定义 UITableViewCell 中
【发布时间】:2015-10-17 08:38:43
【问题描述】:

我有 NSMutableArray ,它包含字符串、日期时间和UIImage一切都显示在我的 UITableView 中,没有图像

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *cellIdentifier =@"Cell";
        CustomCell *customCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

        contact = res[indexPath.row];

        NSString *titleNews = contact[@"title"];
        NSString *newsDateTime = contact[@"datetime"];
        NSString *NewsImageName = contact[@"featured_image"];

        UIImage *image = [UIImage imageNamed:NewsImageName ];

        customCell.customFirstNameLabel.text = titleNews;
        customCell.customLastnameLabel.text = newsDateTime;
        customCell.customImageView.image =image;

        return customCell;
    }

我正在尝试从这里显示数据。

{
        datetime = "2015-09-10 13:35:33";
        "featured_image" = "http://198.72.115.125/~pratidin/assets/news_images/2015/09/10/thumbnails/bdp-001.jpg";
        "item_id" = 102235;
        "main_news_url" = "http://198.72.115.125/~pratidin/api/detailnews/102235";
        "main_url" = "http://198.72.115.125/~pratidin/api/topnews";
        summery = "\U0997\U09a4 \U0995\U09af\U09bc\U09c7\U0995\U09ae\U09be\U09b8 \U09af\U09be\U09ac\U09a4 \U09ae\U09cb\U09ac\U09be\U0987\U09b2\U09aa\U09cd\U09b0\U09c7\U09ae\U09c0\U09b0\U09be \U0985\U09aa\U09c7\U0995\U09cd\U09b7\U09be\U09af\U09bc…";
        title = "\U098f\U09ac\U09be\U09b0 \U0986\U0987\U09ab\U09cb\U09a8 \U09ec\U098f\U09b8 \U0993 \U09ec\U098f\U09b8 \U09aa\U09cd\U09b2\U09be\U09b8";
    }

这是我的看法。 有人能帮我解决这个问题吗

【问题讨论】:

  • @EICaptain 检查..我提供了更多细节
  • 它的imageurl...如果它的图像url,你不能直接用imagename设置图像

标签: objective-c iphone uitableview nsmutablearray


【解决方案1】:

它的图片url……你不能直接通过图片名设置图片……你必须像这样从url获取数据……

NSURL *imgURL = [NSURL URLWithString:NewsImageName];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSData *imgData = [NSData dataWithContentsOfURL:imgURL];

  dispatch_async(dispatch_get_main_queue(), ^{
        imageView.image = [UIImage imageWithData:imgData];
   });
});

或者直接设置

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL: NewsImageName]];

【讨论】:

【解决方案2】:

这里您的数据源联系人[@"featured_image"] 是一个网址。所以你必须从 url 加载图像。你可以使用 SDWebImage.framework 来加载图片

            [cell.imageview sd_setImageWithURL:[NSURL URLWithString:contact[@"featured_image"]] placeholderImage:nil options:SDWebImageCacheMemoryOnly completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                if (image) {
                     [cell.imageview setImage:image];
                }
            }];

【讨论】:

    【解决方案3】:

    您必须先从 URL 加载图像内容:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *cellIdentifier =@"Cell";
        CustomCell *customCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    
        contact = res[indexPath.row];
    
        NSString *titleNews = contact[@"title"];
        NSString *newsDateTime = contact[@"datetime"];
        NSString *NewsImageName = contact[@"featured_image"];
    
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:NewsImageName]]];
        //UIImage *image = [UIImage imageNamed:NewsImageName ];
    
        customCell.customFirstNameLabel.text = titleNews;
        customCell.customLastnameLabel.text = newsDateTime;
        customCell.customImageView.image =image;
    
        return customCell;
    }
    

    请注意,这将是图像的同步获取。我建议你检查 LazyTableImages 函数:https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html 或任何图像缓存库,如https://github.com/rs/SDWebImage

    【讨论】:

    • 我只知道一个想法。我只想使用此信息加载其他视图.. customCell.customFirstNameLabel.text = titleNews; customCell.customLastnameLabel.text = newsDateTime; customCell.customImageView.image =图像;用户点击后 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{}
    【解决方案4】:

    请 NSLog 你的图片,以及 imageView 的框架。

    [UIImage imageNamed:NewsImageName ]; 仅从主包加载,看起来您正在从互联网加载图像。

    另一种可能是你设置了错误的框架,所以它消失了。

    你应该这样写

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];
    
    UIImage *img = [[UIImage alloc] initWithData:data];
    

    【讨论】:

      【解决方案5】:

      试试这个

      UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL: NewsImageName]];
      

      【讨论】:

        猜你喜欢
        • 2016-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多