Swift UICollectionView header 显示不正确
Swift UICollectionView header not appearing properly
UICollectionView 的 headers 显示不正确。我在 headers 中添加了一个 UILabel,它应该显示部分编号。当我调试 viewForSupplementaryElementOfKind 时,一切看起来都很好。我查看了有关 collectionView headers 的不同教程,但在我的代码中找不到错误。
完整代码如下:
import UIKit
class ViewController: UIViewController {
var collectionView:UICollectionView!;
override func viewDidLoad() {
super.viewDidLoad()
let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout();
layout.sectionInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20);
layout.itemSize = CGSize(width: 60, height: 60);
layout.headerReferenceSize = CGSize(width: CGRectGetWidth(self.view.bounds), height: 50);
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout);
collectionView.dataSource = self;
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell");
collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header");
collectionView.delegate = self;
self.view.addSubview(collectionView);
}
}
extension ViewController:UICollectionViewDataSource{
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5;
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 10
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath);
cell.backgroundColor = UIColor.whiteColor();
return cell;
}
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
let view = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "header", forIndexPath: indexPath)
view.backgroundColor = UIColor.blueColor();
let label = UILabel(frame: view.frame);
label.text = String(indexPath.section);
label.font = UIFont(name: "helvetica", size: 40);
label.textAlignment = .Center;
view.addSubview(label);
return view;
}
}
替换行
let label = UILabel(frame: view.frame);
和
let label = UILabel(frame: view.bounds);
不过您应该注意到,您需要修复代码中有关重用补充视图的其他问题。我建议创建一个 UICollectionReusableView
的子类,其中包含一个标签,而不是从您的数据源手动添加新标签。
UICollectionView 的 headers 显示不正确。我在 headers 中添加了一个 UILabel,它应该显示部分编号。当我调试 viewForSupplementaryElementOfKind 时,一切看起来都很好。我查看了有关 collectionView headers 的不同教程,但在我的代码中找不到错误。
完整代码如下:
import UIKit
class ViewController: UIViewController {
var collectionView:UICollectionView!;
override func viewDidLoad() {
super.viewDidLoad()
let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout();
layout.sectionInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20);
layout.itemSize = CGSize(width: 60, height: 60);
layout.headerReferenceSize = CGSize(width: CGRectGetWidth(self.view.bounds), height: 50);
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout);
collectionView.dataSource = self;
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell");
collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header");
collectionView.delegate = self;
self.view.addSubview(collectionView);
}
}
extension ViewController:UICollectionViewDataSource{
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5;
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 10
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath);
cell.backgroundColor = UIColor.whiteColor();
return cell;
}
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
let view = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "header", forIndexPath: indexPath)
view.backgroundColor = UIColor.blueColor();
let label = UILabel(frame: view.frame);
label.text = String(indexPath.section);
label.font = UIFont(name: "helvetica", size: 40);
label.textAlignment = .Center;
view.addSubview(label);
return view;
}
}
替换行
let label = UILabel(frame: view.frame);
和
let label = UILabel(frame: view.bounds);
不过您应该注意到,您需要修复代码中有关重用补充视图的其他问题。我建议创建一个 UICollectionReusableView
的子类,其中包含一个标签,而不是从您的数据源手动添加新标签。