UICollectionView没有显示 - bug?

时间:2015-05-23 20:39:50

标签: ios swift uicollectionview uicollectionviewlayout

我对swift有点新手。我试图简单地创建一个放置在屏幕特定区域内的集合视图(风景iPad iOS)。实际上可能存在collectionView错误...以下代码生成附加图像。问题是整个集合视图都不会显示......

import UIKit

class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

    var collectionView: UICollectionView?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 300, left: 500, bottom: 30, right: 10)
        layout.itemSize = CGSize(width: 32, height: 32)
        collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
        collectionView!.dataSource = self
        collectionView!.delegate = self
        collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
        collectionView!.backgroundColor = UIColor.whiteColor()
        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 40
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! UICollectionViewCell
        cell.backgroundColor = UIColor.orangeColor()
        return cell
    }
}

iOS simulator

另外,有没有办法可以避免使用FLOW LAYOUT?刚性集合视图很好。我需要一个10x10的集合视图,其大小或单元数不会变化。

1 个答案:

答案 0 :(得分:1)

对于您尝试使用自动布局听到的内容,这将是您最好的选择。您也可以将UICollectionView的框架设置在右下角,如下所示:

collectionView!.frame = CGRect(x: self.view.frame.size.width - collectionView!.frame.size.width, y: self.view.frame.size.height - collectionView!.frame.size.height, width: 400, height: 400)

但这更像是一个临时修复,必须考虑每个设备和未来设备的布局。