2015-12-21 77 views
0

我有一個自定義UICollectionViewCell類,我想添加子視圖。如何添加子視圖到自定義UICollectionViewCell

我的單元格類:SetUpView()方法將添加我需要的所有子類。

import Foundation 
import UIKit 

class RecipeCell: UICollectionViewCell { 

    var RecipeImg: UIImage! 
    var StarRatingImg: UIImage! 
    var RecipeTitleText = "" 
    var RecipeTextDescription = "" 


    var View: UIView! 
    var ImageContainer: UIImageView! 
    var FavIcon: UIImageView! 
    var StarRatingContainer: UIImageView! 
    var KCAL: UILabel! 
    var RecipeTitle: UITextView! 
    var RecipeText: UITextView! 


    func SetUpView() 
    { 
     //DropDown!.backgroundColor = UIColor.blueColor() 
     self.translatesAutoresizingMaskIntoConstraints = false 

     //View for recipe 
     View = UIView(frame: CGRectMake(0, 0, self.frame.width, self.frame.height)) 
     View.backgroundColor = UIColor.whiteColor() 

     //Recipe image 
     ImageContainer = UIImageView(frame: CGRectMake(0, 0, View.frame.width, View.frame.height/2)) 
     ImageContainer.image = RecipeImg 
     ImageContainer.contentMode = .ScaleToFill 

     //Recipe favorit icon 
     FavIcon = UIImageView(frame: CGRectMake(ImageContainer.frame.width - 35, 5, 30, 30)) 
     FavIcon.image = UIImage(named: "LikeHeart") 

     //Star rating image 
     StarRatingContainer = UIImageView(frame: CGRectMake(10, ImageContainer.frame.height + 5, ImageContainer.frame.width - 20, (View.frame.height/2) * (1/5))) 
     StarRatingContainer.image = StarRatingImg 
     StarRatingContainer.contentMode = .ScaleAspectFit 

     //RecipeTitle container 
     RecipeTitle = UITextView(frame: CGRectMake(10, StarRatingContainer.frame.height + ImageContainer.frame.height + 10, View.frame.width - 20, 30)) 
     RecipeTitle.font = UIFont(name: "OpenSans-Semibold", size: 12) 
     //RecipeTitle.backgroundColor = UIColor.redColor() 
     RecipeTitle.editable = false 
     RecipeTitle.text = RecipeTitleText 
     RecipeTitle.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0) 


     //RecipeText container 
     RecipeText = UITextView(frame: CGRectMake(10, StarRatingContainer.frame.height + ImageContainer.frame.height + RecipeTitle.frame.height + 15, View.frame.width - 20, 50)) 
     RecipeText.font = UIFont(name: "OpenSans", size: 12) 
     //RecipeText.backgroundColor = UIColor.grayColor() 
     RecipeText.editable = false 
     RecipeText.text = RecipeTextDescription 
     RecipeText.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0) 

     //KCAL label 
     KCAL = UILabel(frame: CGRectMake(15, StarRatingContainer.frame.height + ImageContainer.frame.height + RecipeTitle.frame.height + RecipeText.frame.height + 20, 200, 20)) 
     KCAL.text = "420 KCAL. PER. PORTION" 
     KCAL.font = UIFont(name: "OpenSans-Bold", size: 10) 
     KCAL.textColor = UIColor(CGColor: "#dc994a".CGColor) 

     //Adding the views 
     self.addSubview(View) 
     View.addSubview(ImageContainer) 
     View.addSubview(KCAL) 
     View.addSubview(StarRatingContainer) 
     View.addSubview(RecipeTitle) 
     View.addSubview(RecipeText) 
     ImageContainer.addSubview(FavIcon) 

     View.bringSubviewToFront(ImageContainer) 

    } 

} 

我有一個使用自定義的小區類,UICollectionView。

創建我在viewDidLoad中UICollectionView()

// Create Collection view 
     layout = UICollectionViewFlowLayout() 
     layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0) 
     layout.itemSize = CGSize(width: screenWidth/MenuViewConst - 1, height: screenWidth - 1) 
     layout.minimumInteritemSpacing = 1 
     layout.minimumLineSpacing = 1 

     collectionView = UICollectionView(frame: CGRect(x: 0, y: 105, width: self.view.frame.width, height: self.view.frame.height - 150), collectionViewLayout: layout) 
     collectionView?.tag = 5 
     collectionView!.dataSource = self 
     collectionView!.delegate = self 
     collectionView!.registerClass(RecipeCell.self, forCellWithReuseIdentifier: "CollectionViewCell") 
     collectionView!.backgroundColor = UIColor.lightGrayColor() 
     collectionView!.contentInset.top = 0 

在cellForItemAtIndexPath委託我成立了UICollectionView使用我的自定義單元格類。但是我不能在我的自定義單元類中調用SetUpView()方法,因爲這會繼續在子視圖上添加子視圖。我無法弄清楚在進入委託之前如何將子視圖添加到UICollectionViewCell。希望你們能幫助 - 謝謝你

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! RecipeCell 

     let recipe = self.RecipeArr[indexPath.row] 

     cell.backgroundColor = UIColor.grayColor() 
     cell.layer.borderColor = UIColor.whiteColor().CGColor 
     cell.layer.borderWidth = 0.5 
     cell.RecipeImg = UIImage(named: "Burger") 
     cell.StarRatingImg = UIImage(named: "StarRating") 
     cell.RecipeTitleText = recipe["name"].string! 
     cell.RecipeTextDescription = recipe["instruction"].string! 

     //BAD IDEA! 
     //cell.SetUpView() 

     print("new cell") 

     return cell 

    } 

回答

1

您需要使用init(frame: CGRect)繼承功能的UICollectionViewCell

class RecipeCell: UICollectionViewCell { 
    var imageView : UIImageView? 
    override init(frame: CGRect) { 
    super.init(frame: frame) 
    //initialize all your subviews. 
    imageView = UIImageView() 
    } 
} 

也不要忘記註冊您的自定義類的viewDidLoad功能

collectionView!.registerClass(RecipeCell.self, forCellWithReuseIdentifier: "CollectionViewCell") 

和你的CollectionView代表會是這樣

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! RecipeCell 
    cell.imageView.image = UIImage(named:"yourImage.png") 
} 
+0

感謝您respons @zumrymohamed。我試過(initwithframe),但屬性沒有在cellForItemAtIndexPath中設置。例如:cell.RecipeImg = UIImage(命名爲「Burger」),下面的屬性沒有設置。 –

+0

正確檢查編輯後的答案。你應該用「init」方法初始化你的imageview。 –

相關問題