Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift中设置UICollectionView单元格高度_Ios_Swift_Uicollectionview - Fatal编程技术网

Ios 在Swift中设置UICollectionView单元格高度

Ios 在Swift中设置UICollectionView单元格高度,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我正在尝试创建一个UICollectionView,其中每个单元格将根据单元格中的内容量具有不同的高度。我理解这一点 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize 调用以在单元格出列时设置单个单元格的大小。我正

我正在尝试创建一个UICollectionView,其中每个单元格将根据单元格中的内容量具有不同的高度。我理解这一点

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
调用以在单元格出列时设置单个单元格的大小。我正在计算牢房需要的高度

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
但是我创建的变量没有保存在sizeForItemAtIndexPath中

我哪里做错了?下面是完整的课程。我一直在胡闹,试图让这个工作,所以可能有一些不必要的代码浮动。得到任何帮助都是感激的

import UIKit
var cellHeight: CGFloat = 0.0
class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

    var collectionView: UICollectionView?
    internal var thisCellHeight: CGFloat = CGFloat()



    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let screenSize: CGRect = UIScreen.mainScreen().bounds
        let screenWidth = screenSize.width
        let screenHeight = screenSize.height


        var thisWidth = screenWidth - 10

        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 70, left: 10, bottom: 10, right: 10)
        layout.itemSize = CGSize(width: thisWidth, height: 180)
        collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
        //collectionView!.backgroundView?.backgroundColor = UIColor.blueColor()
        collectionView!.dataSource = self
        collectionView!.delegate = self
        collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
        collectionView!.backgroundColor = hexStringToUIColor("#15ADFF")
        self.view.addSubview(collectionView!)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 14
    }

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


        println("Index Path Number: \(indexPath)")
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as UICollectionViewCell
        cell.backgroundColor = UIColor.whiteColor()


        var typeLabel = UILabel(frame: CGRectMake(10, 0, 200, 21))
        typeLabel.text = "Card Type"
        typeLabel.font = typeLabel.font.fontWithSize(20)
        cell.addSubview(typeLabel)

        var titleLabel = UILabel(frame: CGRectMake(10, 25, 250, 21))

        titleLabel.text = "This Is The Article Title. This Is The Article Title. This Is The Article Title. This Is The Article Title. This Is The Article Title. This Is The Article Title. This Is The Article Title. This Is The Article Title. This Is The Article Title. This Is The Article Title. This Is The Article Title. This Is The Article Title. This Is The Article Title. This Is The Article Title. This Is The Article Title. This Is The Article Title. This Is The Article Title. This Is The Article Title. This Is The Article Title. This Is The Article Title. This Is The Article Title. This Is The Article Title."
        titleLabel.font = titleLabel.font.fontWithSize(20)
        titleLabel.numberOfLines = 0
        titleLabel.sizeToFit()
        thisCellHeight = CGFloat(titleLabel.frame.height)
        println("The title text is \(cellHeight) high")
        cell.addSubview(titleLabel)

        var authorLabel = UILabel(frame: CGRectMake(10, thisCellHeight + 30, 200, 21))
        authorLabel.text = "This Is The Article Author"
        authorLabel.font = authorLabel.font.fontWithSize(15)
        cellHeight = cellHeight + CGFloat(authorLabel.frame.height)
        println("After article added height \(cellHeight) high")
        cell.addSubview(authorLabel)

        var timestampLabel = UILabel(frame: CGRectMake(10, thisCellHeight + 50, 200, 21))
        timestampLabel.text = "This Is The Timestamp"
        timestampLabel.font = timestampLabel.font.fontWithSize(15)
        cellHeight = cellHeight + CGFloat(timestampLabel.frame.height)
        println("After timestamp added height \(cellHeight) high")
        cell.addSubview(timestampLabel)

        return cell
    }

    func hexStringToUIColor (hex:String) -> UIColor {
        var cString:String = hex.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet() as NSCharacterSet).uppercaseString

        if (cString.hasPrefix("#")) {
            cString = cString.substringFromIndex(advance(cString.startIndex, 1))
        }

        if (countElements(cString) != 6) {
            return UIColor.grayColor()
        }

        var rgbValue:UInt32 = 0
        NSScanner(string: cString).scanHexInt(&rgbValue)

        return UIColor(
            red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
            green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
            blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
            alpha: CGFloat(1.0)
        )
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {



        println("Passed height = \(cellHeight)")

        return CGSizeMake(270, 300)


    }



}

错误之处在于试图计算cellForItemAtIndexPath中的单元格高度。您应该在SizeFormatindeXPath中执行此操作,就像在表视图中的HeightForRowatineXpath中执行此操作一样。cellForItemAtIndexPath中的代码也不好,因为在重复使用单元格时,将多次添加所有这些标签。您应该在自定义单元格类的init方法中添加所有标签。