gpt4 book ai didi

ios - 检查 UICollectionViewCell 子类中的变量是否为 nil

转载 作者:搜寻专家 更新时间:2023-10-31 22:26:49 25 4
gpt4 key购买 nike

我有这个 UICollectionViewcell

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

if let CurrentPost = posts[indexPath.row] as? Post {
if(CurrentPost.PostImage == nil) {
print(CurrentPost.PostText)
}
}

return cell
}

class PostCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
designCell()
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

let postImage: UIImageView = {
let v = UIImageView()
v.translatesAutoresizingMaskIntoConstraints = false
v.contentMode = .scaleAspectFill
return v
}()

func designCell() {
addSubview(postImage)

if (postImage.image == nil) {
print("ss")
}

cellConstraints()
}
}

现在我想做的是检查 posts[indexPath.row] PostImage 是否为 nil。如果它是 nil 那么在 PostCell 我想打印“ss”,否则我想将 postImage 的图像设置为 PostImage

最佳答案

在你的手机里:

func passImage(imageToSet: UIImage?){
if let image = imageToSet{
//Do whatever you want
}else{
print("ss")
}
}

在你的VC中

if let CurrentPost = posts[indexPath.row] as? Post{
cell.passImage(imageToSet: CurrentPost.PostImage)
}

关于ios - 检查 UICollectionViewCell 子类中的变量是否为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45480082/

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