【问题标题】:Collection view images are not showing but the Url works fine集合视图图像未显示,但 URL 工作正常
【发布时间】:2017-11-16 00:40:52
【问题描述】:

我正在尝试从服务器获取图像并在集合视图中显示它,因为我给了背景颜色红色我得到纯红色单元格 bt 我有一个图像视图和一个标签,它们都没有显示!!

我从服务器 self.iname 获取图像名称是一个数组,我在其中存储了图像名称!!这个 url 的东西有效,因为我在前视图控制器中将它用于个人资料图片。所以我的有什么问题代码为什么我没有获得图像视图?提前感谢您的建议!是的,我已经给出了我的数据源和代表,并给出了我所有的出口连接。 我正在使用故事板!

这是我的收藏视图块-

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{


[collectionView registerClass:[ColCell class] forCellWithReuseIdentifier:@"cell"];

ColCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

[[[cell contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

UIView * contents=[[UIView alloc]      initWithFrame:cell.contentView.bounds];

[contents setBackgroundColor:[UIColor clearColor]];

[cell.contentView addSubview:contents];

if([self.iname count]!=0){

[cell setTag:indexPath.row];    // set tag to the indexPath.row so we can access it later

// add interactivity
UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onButtonTapped:)];

[tap setNumberOfTapsRequired:1];

[cell addGestureRecognizer:tap];

NSString *fileName = [NSString stringWithFormat:@"%@",self.iname]; //objectAtIndex:indexPath];

NSLog(@"%@",fileName);

NSString *baseurl=[NSString stringWithFormat:@"http://test.kre8tives.com/barebon/upload/"];

NSDictionary *dict = self.iname[indexPath.row];

NSLog(@"%@", [self.iname objectAtIndex: indexPath.row]);

NSString *paths = [NSString stringWithFormat:@"%@%@", baseurl, dict];

NSLog(@"@@@@%@",paths);

[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:paths]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

    NSLog(@"%@",response);

    cell.dimg.image=[[UIImage alloc] initWithData:data];
   // UIImage *imgage = [[UIImage alloc] initWithData:data];

   // cell.dimg.image=imgage;
    cell.lbl.text=[self.iname objectAtIndex:indexPath.row];
    NSLog(@"%@",[self.iname objectAtIndex:indexPath.row]);
}];
}
    //cell.dimg.image=[[UIImage alloc]initWithData:data];
     if (cell.selected) {
       cell.backgroundColor = [UIColor blueColor]; // highlight selection
     }
     else
     {
         cell.backgroundColor = [UIColor redColor]; // Default color
     }
    return cell;
}

这是我的 collectionviewcell 文件 (colcell.h)

#import <UIKit/UIKit.h>

@interface ColCell : UICollectionViewCell
@property (strong, nonatomic) IBOutlet UIImageView *dimg;
@property (strong, nonatomic) IBOutlet UILabel *lbl;

@end

这是我的 collectionviewcell 文件 .m (colcell.m)

 #import "ColCell.h"

 @implementation ColCell

-(void)awakeFromNib {
    UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onButtonTapped:)];
    [tap setNumberOfTapsRequired:1];
    [self addGestureRecognizer:tap];
    [super awakeFromNib];
}

-(void)onButtonTapped:(id)sender
{
 //the response to the gesture.
//mind that this is done in the cell. If you don't want things to happen from this cell.
//then you can still activate this the way you did in your question.

}
@end

这是我的回应之一

2017-06-14 09:03:03.647 MenuBar[754:23050] <NSHTTPURLResponse: 0x60000023c5c0> { URL:   http://test.kre8tives.com/barebon/upload/impotedbeernew.png } { status code: 200, headers {
"Accept-Ranges" = bytes;
"Cache-Control" = "max-age=2592000";
"Content-Length" = 8261;
"Content-Type" = "image/png";
Date = "Tue, 13 Jun 2017 12:04:47 GMT";
Etag = "\"31e1206-2045-551704d9ee800\"";
Expires = "Thu, 13 Jul 2017 12:04:47 GMT";
"Last-Modified" = "Thu, 08 Jun 2017 10:27:44 GMT";
Server = "Apache Phusion_Passenger/4.0.10 mod_bwlimited/1.4 mod_fcgid/2.3.9";
Vary = "User-Agent";
} }
2017-06-14 09:03:03.647 MenuBar[754:23050] impotedbeernew.png
2017-06-14 09:03:03.648 MenuBar[754:23050] <NSHTTPURLResponse:   0x60000023e360> { URL:     http://test.kre8tives.com/barebon/upload/blendedwhiskyne.png } { status code: 200, headers {
    "Accept-Ranges" = bytes;
    "Cache-Control" = "max-age=2592000";
    "Content-Length" = 17188;
    "Content-Type" = "image/png";
    Date = "Tue, 13 Jun 2017 12:04:47 GMT";
    Etag = "\"31e118d-4324-5517150204640\"";
    Expires = "Thu, 13 Jul 2017 12:04:47 GMT";
    "Last-Modified" = "Thu, 08 Jun 2017 11:40:01 GMT";
    Server = "Apache Phusion_Passenger/4.0.10 mod_bwlimited/1.4 mod_fcgid/2.3.9";
    Vary = "User-Agent";
 } }

【问题讨论】:

  • 如果显示图片,这段代码也是错误的,你会在cell中看到错误的图片,(因为UICollectionView的cell-reuse系统)。 SDWebImage可以帮你https://github.com/rs/SDWebImage[imageView sd_setImageWithURL:imageUrl]
  • 我几乎已经完成了这个 n 并且我尝试添加这个库但它没有很好地工作所以我使用了我自己的方法!!@DungTran
  • 我认为您注册colcell 的方式有点错误。注册应该在 collectionview 的初始化时发生,而不是在它已经被调用时发生
  • @Joshua 你建议我应该怎么做??
  • @DungTran 许多人说如果你使用故事板就不需要注册

标签: ios objective-c xcode uiimageview uicollectionview


【解决方案1】:

最后,删除这些代码将适用于 @Akshay。

[[[cell contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
UIView * contents=[[UIView alloc] initWithFrame:cell.contentView.bounds];
[contents setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:contents];

因为它们是未使用的代码,并且第一行代码删除了标签 lbl 和 imageView dimg,所以它们不会显示。

【讨论】:

    猜你喜欢
    • 2013-02-04
    • 2017-08-13
    • 2014-12-18
    • 2020-06-10
    • 1970-01-01
    • 2014-04-07
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多