2016-11-15 88 views
-1

我試圖創建UicollectionView並在該單元格中加載圖像。我只獲取單元格而不是圖像。UiCollectionView - 從url加載圖片

- (void)viewDidLoad  
{ 
    [super viewDidLoad]; 
    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init]; 
    collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(10, 30, self.view.frame.size.width-20, self.view.frame.size.height)collectionViewLayout:layout]; 
; 
    [collectionView setDataSource:self]; 
    [collectionView setDelegate:self]; 

    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"]; 
    [collectionView setBackgroundColor:[UIColor blackColor]]; 

    [self.view addSubview:collectionView]; 

} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    return 8; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *identifier = @"cookcell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 

    if (!cell) { 
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 
    } 

    NSURL *url = [NSURL URLWithString:@"http://img.auctiva.com/imgdata/1/2/8/3/1/8/7/webimg/611377020_tp.jpg"]; 

    NSData *data = [[NSData alloc] initWithContentsOfURL:url]; 
    UIImage *tmpImage = [[UIImage alloc] initWithData:data]; 

    cell.imageView.image = tmpImage ; 
    return cell;; 
} 

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath: 

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

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

    cell.backgroundColor=[UIColor whiteColor]; 

    return cell; 
} 

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return CGSizeMake(80, 80); 
} 

- (void)didReceiveMemoryWarning { 

    [super didReceiveMemoryWarning]; 

    // Dispose of any resources that can be recreated. 
} 
+0

添加的ImageView在收集視圖的數據源的方法''cellForItemAt您 – pkc456

+0

不能添加這樣[的CollectionView addSubview:_imageView]。 –

+0

你試過了什麼,你想在collectionview中顯示圖像的位置。 –

回答

0
  • 確認委託數據源和委託的CollectionView,無論你正在使用它的的viewController。

  • 在委託中,我們將cellForRowAtIndex與UITableView相同。

  • 獲取相應於indexpath的函數中的單元格。

  • 將圖像分配給該單元格,(Cell應具有UIImageView)。如果你想要靜態圖像,你可以直接分配給每個索引路徑的委託方法中的其他單元類別。

0

看到你的代碼,它已經爲收集視圖設置了數據源協議,但那不是從URL加載圖像的方式。你必須實現以下數據源協議方法作爲迅速3.

FUNC的CollectionView的(_的CollectionView:UICollectionView,numberOfItemsInSection部的:int) - >內部{

return datalistArr.count 

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collecionViewCell", for: indexPath) as! CustomCell   
    cell.imageViewIncell.af_setImage(withURL: URL(string: imageurl), placeholderImage: UIImage(named: "placeholderImage")) 
    return cell 
} 

如果你已經使用Alamofire它有UIImageView的擴展,它很容易從URL加載圖像,例如,cell.imageViewIncell.af_setImage(withURL:URL(string:imageurl),placeholderImage:UIImage(named:「placeholderImage」))

0

你需要設置圖像中的cellForRowAtIndexPath:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"receipeCell" forIndexPath:indexPath]; 
    NSURL *url = [NSURL URLWithString:@"http://img.auctiva.com/imgdata/1/2/8/3/1/8/7/webimg/611377020_tp.jpg"]; 

    NSData *data = [[NSData alloc] initWithContentsOfURL:url]; 

    UIImage *tmpImage = [[UIImage alloc] initWithData:data]; 
    cell.imageView.image = tmpImage ; 

    return cell;; 

} 
+0

cell.imageView.image = tmpImage; - 在這一行屬性imageView不在collectionViewcell –

+0

它是默認的imageview不是自定義單元格imageview ..你需要用你的imageview替換這個imageview。 – KKRocks

+0

對不起,我不能得到你。不能將圖像視圖添加到集合視圖儀式中? –