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

Check if variable is nil from UICollectionViewCell subclass

我有这个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 是否为零。如果它是零,那么在 PostCell 我想打印 "ss",否则我想将 postImage 的图像设置为 PostImage

class PostCell: UICollectionViewCell {
    var postImg: UIImage!
    override init(frame: CGRect) {
        super.init(frame: frame)
        designCell()
    }

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

    convenience init(frame: CGRect, img: UIImage) {
        self.postImg = img
        designCell()
    }

    let postImage: UIImageView = {
        let v = UIImageView()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.contentMode = .scaleAspectFill
        v.image = postImg
        return v
    }()
    func getImage(img: UIImage) {
        self.postImg = img
    }
    func designCell(){

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

在你的手机里:

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)
}

首先针对图像可用性创建两种不同的约束调整方法。

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")
        }
    }

    func cellConstraintForImageWithoutText(){
         //Change your constraint if data have image but doesn't have text
    }
    func cellConstraintForImageWithText(){
         //Change your constraint if data have image as well as text
    }
    func cellConstraintForNoImageWithoutText(){
         //Change your constraint if data neither have image nor have text
    }
    func cellConstraintForNoImageWithText(){
         //Change your constraint if data doesn't have image but have text
    }
}

现在只需检查图像是否为 nil 然后调用 NoImage 方法,否则调用其他方法。

let postText = CurrentPost.text

if let img = CurrentPost.PostImage {
    if txt = postText {
        cell.cellConstraintForImageWithText()             
    }else {
        cell.cellConstraintForImageWithoutText()            
    }
}
else {
    if txt = postText {
        cell.cellConstraintForNoImageWithText()             
    }else {
        cell.cellConstraintForNoImageWithoutText()            
    }
}