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

时间:2017-08-03 09:25:02

标签: ios swift uicollectionviewcell

我有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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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)
}

答案 2 :(得分:1)

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

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()            
    }
}
相关问题