微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法在 UICollectionView 单元格中显示图像

如何解决无法在 UICollectionView 单元格中显示图像

我需要在集合视图单元格中显示图像,但是当我尝试这样做时,我得到了 10 个空单元格,我不知道我在哪里犯了错误

这是我的ViewController代码

class NewgalleryViewController: UIViewController {
var presenter: ViewToPresenterPhotoProtocol?
var builder: galleryRequestBuilder?

@IBOutlet var collectionView: UICollectionView!

let reuseIdentifier = "customCVCell"


@objc func refresh() {
    presenter?.refresh()
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.setupPresenterIfNeed()
    presenter?.viewDidLoad()
    // Do any additional setup after loading the view.
}

func setupPresenterIfNeed() {
    self.collectionView.backgroundColor = UIColor.white
    if self.presenter == nil {
        let presenter = galleryPresenter()
        presenter.view = self
        self.presenter = presenter
        self.builder = galleryRequestBuilder()
    }
}
}

     extension NewgalleryViewController: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
    return self.presenter?.photos.count ?? 0
}

func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier,for: indexPath) as! PhotoCollectionViewCell
    
    KFImage.url(builder?.createImageUrl(name: (presenter?.photos[indexPath.item].name)!))
        .onSuccess { result in
            cell.imageView.image = result.image
        }
    
    return cell
}

func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeforItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: 180,height: 128)
}

func collectionView(_ collectionView: UICollectionView,minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 1.0
}

func collectionView(_ collectionView: UICollectionView,layout
                        collectionViewLayout: UICollectionViewLayout,minimumLinespacingForSectionAt section: Int) -> CGFloat {
    return 20.0
}
// MARK: - UICollectionViewDelegate protocol

private func collectionView(collectionView: UICollectionView,didSelectItemAtIndexPath indexPath: NSIndexPath) {
    // handle tap events
    print("You selected cell #\(indexPath.item)!")
}
}

extension NewgalleryViewController: PresenterToViewPhotoProtocol{
func onFetchPhotoSuccess() {
    self.collectionView.reloadData()
    self.collectionView!.collectionViewLayout.invalidateLayout()
    self.collectionView!.layoutSubviews()
    self.collectionView.refreshControl?.endRefreshing()
}

func onFetchPhotoFailure(error: String) {
    print("View receives the response from Presenter with error: \(error)")
    self.collectionView.refreshControl?.endRefreshing()
}


}

这是单元格的代码

class PhotoCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var imageView: UIImageView!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

}

我已经检查了我向其发出请求的链接,并且它有效。所以问题不在于链接。也许我应该在获取图像后重新加载项目?

解决方法

您应该在加载视图后设置您的 override func viewDidLoad() { super.viewDidLoad() // Add this lines collectionView.delegate = self collectionView.dataSource = self self.setupPresenterIfNeed() presenter?.viewDidLoad() } 委托和数据源:

t1_3

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?