如何将子视图添加到自定义UICollectionViewCell [英] How do I add subviews to a custom UICollectionViewCell

查看:71
本文介绍了如何将子视图添加到自定义UICollectionViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的UICollectionViewCell类,我想添加子视图。

I have a custom UICollectionViewCell class where I want to add subviews.

我的单元格类:SetUpView()方法将添加我需要的所有子程序。

My cell class: The SetUpView() method will add all the subvies I need.

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。

I have a UICollectionView which uses the custom cell class.

我在viewDidLoad()中创建我的UICollectionView

I create my UICollectionView in viewDidLoad()

// 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。希望你们能帮忙 - 谢谢你们

In cellForItemAtIndexPath delegate I set up the UICollectionView to use my custom cell class. But I can't call the SetUpView() method from my custom cell class here, because that will just keep adding subviews on subviews. I can't figure out how to add the subviews to the UICollectionViewCell before entering the delegate. Hope you guys can help - Thank you

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

    }


推荐答案

你需要使用 init(frame:CGRect) UICollectionViewCell 中的继承函数。

You need to use init(frame: CGRect) inherited function in the UICollectionViewCell .

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

不要忘记在 viewDidLoad 函数中注册自定义类

also don't forget to register your custom class in the viewDidLoad function

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

并且你的collectionview委托就像这个

and your collectionview delegate would be like this

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

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

这篇关于如何将子视图添加到自定义UICollectionViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆