带有图像数组的UICollectionView将其设置为正确

时间:2017-08-09 12:56:41

标签: ios swift uicollectionview uiimage uicollectionviewcell

我有一个问题。在UICollectionView内部我有内容模式的UIImage - Aspect Fit ...现在我需要在UICollectionView图像中显示一个接近另一个。现在我们之间有很多空间。

On Storyboards

On simulator

extension UserDetailInformationOfAnimalViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

//MARK: - Collections
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    let post = postsArray[collectionView.tag]
    //        print("count: ", post.imagesLink.count)
    return post.imageLinks.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell

    let post = postsArray[collectionView.tag]
    //        print("postArrayPhotos: ", post.imagesLink.count)
    if post.imageLinks.count != 0 {
        //            print("POST = ", post)
        //            print("Look here: ", post.imagesLink[indexPath.row].imageLink)
        //            print("IMAGELINK = ", post.imagesLink[indexPath.row].imageLink)
        let imageLink = post.imageLinks[indexPath.row]

        if imageLink.imageLink != nil {
            let url = URL(string: imageLink.imageLink!)
            print("IMAGELINK = ", imageLink)

            cell.imageOfAnimalInCollectionView.sd_setImage(with: url!, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in
            })
        }
    }

    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let height = self.view.frame.size.height;
    let width  = self.view.frame.size.width;
    // in case you you want the cell to be 40% of your controllers view
    return CGSize(width: width, height: height * 0.35)
  }
}

2 个答案:

答案 0 :(得分:0)

使用此方法动态设置单元格的宽度

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    if indexPath.row == whatever you want {
        return CGSize(width: collectionView.frame.size.width/2, height: collectionView.frame.size.width/2)
    }
}

答案 1 :(得分:0)

我理解你的问题。在这里,我给你一个解决方法。此解决方案可能对您无效,但它会让您了解如何解决它。主要概念是您必须根据图像返回单元格大小。但是瞬间的形象并不在你手中。它在服务器上。所以你可以做那个拳头返回演示或占位符图像高度然后在完成从服务器下载实际图像后告诉集合视图根据实际图像大小重新布局单元格。

 cell.imageOfAnimalInCollectionView.sd_setImage(with: url!, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in
           //told the collection view to relayout according to image size
           collectionView.collectionViewLayout.invalidateLayout()          
    })

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let cell = collectionView.cellForItem(at: indexPath) as! CollectionViewCell
        return cell.imageOfAnimalInCollectionView.image.size! // Put your logic here, until actual image download from server you can return a default size.
    }