3

I have got this 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()
    }
}

Now what I want to do is to check is at posts[indexPath.row] the PostImage is nil or not. If it is nil then at PostCell I want to print "ss", otherwise I want to set postImage's image to PostImage.

Anh Pham
  • 2,108
  • 9
  • 18
  • 29
sakoaskoaso
  • 347
  • 4
  • 17

3 Answers3

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()
    }
}
Rishabh
  • 465
  • 5
  • 14
  • But i need it in `PostCell` so i can change constraints and stuff – sakoaskoaso Aug 03 '17 at 09:29
  • Oh, for that you will have to create a method to pass your image into the PostCell class. Maybe you should add another parameter in init method for taking an uiimage object and then check that uiimage object. – Rishabh Aug 03 '17 at 09:31
  • adding another parameter gives me an error: `Initializer does not override a designated initializer from its superclass` – sakoaskoaso Aug 03 '17 at 09:34
  • `getImage` gets called after the class is called,so it automatically gives it a value of nil` – sakoaskoaso Aug 03 '17 at 09:43
  • you should try to create a custom init and call self.init inside that function. https://stackoverflow.com/questions/24302288/how-to-write-init-method-in-swift – Rishabh Aug 03 '17 at 09:52
  • i tried to create my own initializer like this: `convenience init(frame: CGRect,image:UIImage?) { self.init(frame: frame) backgroundColor = .white designCell() } ` but it gave me an error saying that `self.init(frame: frame)` should be `self.init(coder: frame)` then another error saying that coder and frame are different things – sakoaskoaso Aug 03 '17 at 09:54
1

Inside your cell:

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

In your VC

if let CurrentPost = posts[indexPath.row] as? Post{
    cell.passImage(imageToSet: CurrentPost.PostImage)
}
Phyber
  • 1,368
  • 11
  • 25
1

First create two different methods for constraint adjustment with respect to image availability.

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

Now just check if image is nil then call NoImage method else call other method.

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()            
    }
}
Syed Qamar Abbas
  • 3,637
  • 1
  • 28
  • 52