gpt4 book ai didi

ios - Collection View 为空时如何显示消息

转载 作者:IT王子 更新时间:2023-10-29 05:32:27 25 4
gpt4 key购买 nike

我试图仅在我的 Collection View 为空时显示一条消息。好吧,当我的 if 为真时,我尝试设置返回 1,但是当我有这个时,它只在我的 Collection View 中显示一个项目(即使我有更多)。当我返回这个(下面的代码)时,它显示了我收藏 View 中的所有项目,但是当我尝试删除它时,最后一个不是“可删除的”,我的意思是,最后一个留在那里。如何仅在我的收藏 View 中没有项目时显示此消息?

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

if self.movies.count > 0 {

self.favCollectionView.backgroundView = nil

return self.movies.count
}
else {
let rect = CGRect(x: 0,
y: 0,
width: self.favCollectionView.bounds.size.width,
height: self.favCollectionView.bounds.size.height)
let noDataLabel: UILabel = UILabel(frame: rect)
noDataLabel.text = "No favorite movies yet."
noDataLabel.textAlignment = .center
noDataLabel.textColor = UIColor.gray
noDataLabel.sizeToFit()

self.favCollectionView.backgroundView = noDataLabel

return 0
}
}

更新

我是这样做的:

func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 { return self.movies.count}
else if section == 1 {
if self.movies.count < 1 { return 1 }
else { return 0 }
}
return 0
}

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

if indexPath.section == 0 {

let movie = movies[indexPath.row]

let imgStg: String = movie.posterURL!
let imgURL: URL? = URL(string: imgStg)
let imgSrc = ImageResource(downloadURL: imgURL!, cacheKey: imgStg)

cell.favTitleLabel.text = movie.title

cell.favPosterImageView.layer.cornerRadius = cell.favPosterImageView.frame.size.width/2
cell.favPosterImageView.clipsToBounds = true

//image cache with KingFisher
cell.favPosterImageView.kf.setImage(with: imgSrc)

return cell

}

else {

let rect = CGRect(x: 0, y: 0, width: self.favCollectionView.frame.width, height: self.favCollectionView.frame.height)
let noDataLabel: UILabel = UILabel(frame: rect)
noDataLabel.text = "No favorite movies yet."
noDataLabel.textAlignment = .center
noDataLabel.textColor = UIColor.gray
noDataLabel.sizeToFit()

let cell = UICollectionViewCell()
cell.contentView.addSubview(noDataLabel)

return cell

}

}

对于@Lamar 的解决方案,但应用程序因错误而崩溃: enter image description here

最佳答案

我在这样的扩展中使用 backgroundView:

extension UICollectionView {

func setEmptyMessage(_ message: String) {
let messageLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.bounds.size.width, height: self.bounds.size.height))
messageLabel.text = message
messageLabel.textColor = .black
messageLabel.numberOfLines = 0;
messageLabel.textAlignment = .center;
messageLabel.font = UIFont(name: "TrebuchetMS", size: 15)
messageLabel.sizeToFit()

self.backgroundView = messageLabel;
}

func restore() {
self.backgroundView = nil
}
}

我这样使用它:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

if (self.movies.count == 0) {
self.collectionView.setEmptyMessage("Nothing to show :(")
} else {
self.collectionView.restore()
}

return self.movies.count
}

关于ios - Collection View 为空时如何显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43772984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com