【问题标题】:Check if variable is nil from UICollectionViewCell subclass检查 UICollectionViewCell 子类中的变量是否为零
【发布时间】:2017-08-03 09:25:01
【问题描述】:

我有这个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 那么在PostCell 我想打印“ss”,否则我想将postImage 的图像设置为PostImage

【问题讨论】:

    标签: ios swift uicollectionviewcell


    【解决方案1】:

    在你的牢房里:

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

    【讨论】:

    • 恐怕这行不通,因为 passImage 在课后被调用
    • 无论你什么时候初始化你的类,它都应该可以工作。
    • 哦,它确实有效,我认为它不会因为它出现在 init 之后。一如既往地感谢!
    【解决方案2】:

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

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

    现在只需检查图像是否为零,然后调用 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()            
        }
    }
    

    【讨论】:

    • 我希望有更短的方法。有五种可能性:有图有文、无图有文、无图无文、有图无文
    • 逻辑上只有四种可能的情况而不是五种
    • 当我回滚时,每个函数都被调用所以它搞砸了
    【解决方案3】:
    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()
        }
    }
    

    【讨论】:

    • 但我需要它在 PostCell 中,所以我可以更改约束和东西
    • 哦,为此您必须创建一个方法来将图像传递到 PostCell 类。也许您应该在 init 方法中添加另一个参数来获取 uiimage 对象,然后检查该 uiimage 对象。
    • 添加另一个参数给我一个错误:Initializer does not override a designated initializer from its superclass
    • getImage 在类被调用后被调用,所以它会自动给它一个 nil 的值`
    • 您应该尝试创建一个自定义初始化并在该函数中调用 self.init。 stackoverflow.com/questions/24302288/…
    猜你喜欢
    • 1970-01-01
    • 2010-09-20
    • 1970-01-01
    • 2012-05-23
    • 2013-11-13
    • 2017-03-06
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    相关资源
    最近更新 更多