简体   繁体   中英

objective-c image not appearing in custom UITableViewCell

I have NSMutableArray , which contains string, datetime, and UIImage . everything is show in my UITableView without image .

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

i am trying show data from here .

{
        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";
    }

this my view . 在此处输入图片说明 would someone help me out how to solve this

Its image url...you can't set image directly by image name...You have to get data from url like this...

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

or directly set

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

Here your data source contact[@"featured_image"] is an url. So you must load image from url. You can use SDWebImage.framework to load image

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

You have to load the image contents from the URL first:

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

Be aware that this will be a synchronous fetching of the images. I suggest you checking the LazyTableImages functions: https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html Or any image caching library like https://github.com/rs/SDWebImage

Please NSLog your image, and the frame of imageView.

And the [UIImage imageNamed:NewsImageName ]; only load from main bundle, it seems like you are loading image from internet.

Another possible is that you have set the wrong frame so it disappeared.

You are supposed to write in this way

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];

UIImage *img = [[UIImage alloc] initWithData:data];

尝试这个

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM