2017-08-03 109 views
3

無我有這個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

回答

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

沒有錯誤,恐怕是行不通的,因爲passImage獲取調用類 – sakoaskoaso

+0

後,它應該工作無論當你初始化你的班級時。 – Phyber

+0

噢,它確實工作,我認爲它不會因爲它是在init之後來的。感謝一如既往! – sakoaskoaso

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

但我需要它'PostCell'這樣我就可以改變的約束和東西 – sakoaskoaso

+0

哦,對,你必須創建你的形象傳遞到PostCell類的方法。也許你應該在init方法中添加另一個參數來獲取uiimage對象,然後檢查uiimage對象。 – Rishabh

+0

增加另一個參數給我一個錯誤:'初始化程序不從superclass' – 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()    
    } 
} 
+0

我希望能有一個更短的way.There五種可能性:用影像和文字,不與文本圖像,沒有圖像沒有文字,沒有用文字 – sakoaskoaso

+0

圖像爲每個場景單獨的函數,然後簡單的調用這些方法... –

+0

邏輯上只有四種可能的情形不是五個 –