2017-06-14 64 views
0

我想從服務器獲取圖像並將其顯示在集合視圖中,因爲我給出了背景色紅色我得到純紅色的單元格bt我有圖像視圖和標籤這兩個都沒有顯示!收集視圖圖像沒有顯示,但網址工作正常

我從服務器self.iname獲取圖像名稱是一個數組,其中我已經存儲圖像名稱!!這個URL的事情工作,因爲我用這個在配置文件圖片在視圖controller.so什麼是錯的我的代碼爲什麼我沒有得到圖像視圖?提前感謝您的建議!!是的,我給了我的數據源和代表,並給予我所有的插座連接。 我正在使用故事板!

這是我收集視圖塊級

- (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"; 
} } 
+0

如果顯示圖像,此代碼也是錯誤的,您將在單元格中看到錯誤的圖像(因爲UICollectionView的單元重用系統)。 'SDWebImage'可以幫助你[https://github.com/rs/SDWebImage](https://github.com/rs/SDWebImage) '[imageView sd_setImageWithURL:imageUrl]' –

+0

我幾乎完成了這個n和我試圖添加這個庫,但它不工作,所以我用我的方法本身!! @ DungTran – Akshay

+0

我認爲它有點錯誤,你怎麼註冊你的'colcell'。註冊應該發生在collectionview的init中,而不是在它已經被叫做 – Joshua

回答

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,因此它們不被顯示。

相關問題