iOS collectionView.sizeForItemAtIndexPath在iPhone 6之前打破任何东西

时间:2015-02-20 09:51:03

标签: ios iphone swift collectionview

我是iOS开发的新手,我正在尝试将我在Android上的项目移植到iOS。我试图在代码中构建一个collectionView作为实现的一部分。问题是,集合视图中每个单元格中的数据可以是不同的字符串大小,每个单元格可以与其邻居不同。我正在尝试附加的代码,但我注意到,iPhone 6要么是物理设备,要么在模拟器中UI看起来像预期的那样......其他任何东西看起来都非常糟糕。它似乎是打破它的sizeForIndexItemAtPath。任何人的想法? TIA

  import UIKit

  class ViewController: UIViewController,       UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

    var collectionView: UICollectionView?
    var thisCellHeight: CGFloat = 0.0

    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: 120)
        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 {



        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, 300, 21))

        titleLabel.text = "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()
        self.thisCellHeight = CGFloat(titleLabel.frame.height)
        println("The title text is \(self.thisCellHeight) 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)
        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)
        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 {


        return CGSizeMake(320, 350)


    }



}

1 个答案:

答案 0 :(得分:9)

您将布局上的项目大小设置为:

layout.itemSize = CGSize(width: thisWidth, height: 120)

您还使用了委托方法

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSizeMake(320, 350)
}

第一个被第二个覆盖。 layout.itemSize是每个项目的默认大小,如果委托方法sizeForItemAtIndexPath未被覆盖。你用一些看起来完全不同的东西来覆盖layout.itemSize。尝试删除委托函数sizeForItemAtIndexPath,这是否符合您的期望?