ios - 如何将 subview 添加到自定义 UICollectionViewCell

标签 ios swift uicollectionview uicollectionviewcell

我有一个自定义的 UICollectionViewCell 类,我想在其中添加 subview 。

我的单元格类: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 委托(delegate)中,我设置了 UICollectionView 以使用我的自定义单元格类。但是我不能在这里从我的自定义单元格类调用 SetUpView() 方法,因为那样只会继续在 subview 上添加 subview 。在进入委托(delegate)之前,我不知道如何将 subview 添加到 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

    }

最佳答案

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

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 delegate 应该是这样的

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

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

关于ios - 如何将 subview 添加到自定义 UICollectionViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34397940/

相关文章:

ios - 自定义相机层上 AVFoundation 中的自动对焦和自动曝光

ios - Swift 4 - 在其 UIViewController 中更新 UIPageViewController 中的变量

ios - 在 `UIImageView` (Swift) 中加载网站预览

swift - 通过 UISearchBar 使用 CoreData 过滤查询

swift - 从子 Collection View 中检索所有值

ios - 从 mapView.annotations 访问 MKAnnotationView

ios - 是否可以为 ios 应用程序开发转弯导航?

swift - 使用变量作为键访问 Swift 字典

UITableViewCell 中的 Swift UICollectionView 无法正常工作

ios - CoreText 属性字符串高度计算不准确