【发布时间】:2015-05-23 20:39:49
【问题描述】:
我是一个 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
}
}
另外,有没有办法可以避免使用 FLOW LAYOUT?严格的集合视图很好。我需要一个大小或单元格数量不变的 10x10 集合视图。
【问题讨论】:
-
您的
UIEdgeInsets巨大。尝试更小的东西。例如,layout.sectionInset = UIEdgeInsets(top: 50, left: 50, bottom: 50, right: 50) -
感谢您的回复-也许我对 UIEdgeInsets 的理解是错误的。您的建议会有所帮助,但可以说我希望我的收藏在右下角 - 这就是我遇到问题的地方。如果我的屏幕尺寸是 1024x768,我会认为我的边缘插图应该类似于(顶部:500,左侧:500,底部:0,右侧:0)......不是吗?这不会让我在右下角看到一个集合视图吗?
-
完美 - 非常有帮助 - 谢谢!
标签: ios swift uicollectionview uicollectionviewlayout