从UICollectionViewCell子类检查变量是否为空

3
我已经拿到了这个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个回答

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

但我需要它在“PostCell”中,这样我就可以更改约束和其他东西。 - sakoaskoaso
哦,为此你将需要创建一个方法来将你的图像传递到PostCell类中。也许你应该在init方法中添加另一个参数,以便接收uiimage对象,然后检查这个uiimage对象。 - Rishabh
添加另一个参数会出现错误:Initializer does not override a designated initializer from its superclass - sakoaskoaso
getImage gets called after the class is called,so it automatically gives it a value of nil` - sakoaskoaso
你应该尝试创建一个自定义的init,并在该函数内调用self.init。 - Rishabh
我尝试创建自己的初始化器,像这样:convenience init(frame: CGRect,image:UIImage?) { self.init(frame: frame) backgroundColor = .white designCell() } 但是它给了我一个错误,说self.init(frame: frame)应该是self.init(coder: frame),然后又出现了另一个错误,说编码器和框架是不同的东西。 - sakoaskoaso

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 在类之后被调用。 - sakoaskoaso
无论何时初始化您的类,它都应该正常工作。 - Phyber
没错,它确实起作用了,我以为它不会起作用,因为它在初始化之后。一如既往地感谢! - sakoaskoaso

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

如果图像为空,则调用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()            
    }
}

我希望有一种更简短的方式。有五种可能性:带图像和文本,不带图像但有文本,不带图像也没有文本,带图像但没有文本。 - sakoaskoaso
从逻辑上讲,只有四种可能情况,而不是五种。 - Syed Qamar Abbas
当我向后滚动时,每个函数都被调用,因此它会出现混乱。 - sakoaskoaso

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接