自定义 UICollectionCell 的 nil 标签
nil label for custom UICollectionCell
EDIT:似乎与 this
的问题相同
我使用自定义布局和单元格制作了集合视图,但我的单元格位置有点偏离,所以我决定对它们进行编号,以便我可以更轻松地调试它。我跟着这个人的 使用标签为我的单元格编号。不知何故 myLabel
为零并导致我的应用程序崩溃。
我相信我已经正确连接了插座,但也许有人可以提供一份清单让我检查我是否做错了什么。
ViewController
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet var gridView: UICollectionView!
private let reuseIdentifier = "DesignCell"
private let numberOfSections = 1
private let numberOfCircles = 48
override func viewDidLoad() {
super.viewDidLoad()
self.gridView.registerClass(MyCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return numberOfSections
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return numberOfCircles
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyCell
cell.myLabel.text = String(indexPath.item) // myLabel is nil and causes a crash
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
GridLayout(我的自定义布局)
import UIKit
import Darwin
class GridLayout: UICollectionViewLayout {
private let numberOfColumns = 12
private let numberOfRows = 4
private var cache = [UICollectionViewLayoutAttributes]()
private var contentHeight: CGFloat = 0
private var contentWidth: CGFloat {
let insets = collectionView!.contentInset
return CGRectGetWidth(collectionView!.bounds) - insets.left - insets.right
}
override func prepareLayout() {
// some calculation i did for my cells
}
override func collectionViewContentSize() -> CGSize {
return CGSize(width: contentWidth, height: contentHeight)
}
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var layoutAttributes = [UICollectionViewLayoutAttributes]()
for attr in cache {
if CGRectIntersectsRect(attr.frame, rect) {
layoutAttributes.append(attr)
}
}
return layoutAttributes
}
}
我的细胞
import UIKit
class MyCell : UICollectionViewCell {
@IBOutlet weak var myLabel: UILabel!
}
尝试在您的 ViewController class:
的 viewDidLoad() 中删除这一行
self.gridView.registerClass(MyCell.self, forCellWithReuseIdentifier: reuseIdentifier)
不需要它,因为您已经在 Storyboard 中创建了 UICollectionView,连接到您的数据源和委托,并且您已经添加了所有必需的方法:
- 节中的项目数量
- cellForItemAtIndexPath
EDIT:似乎与 this
的问题相同
我使用自定义布局和单元格制作了集合视图,但我的单元格位置有点偏离,所以我决定对它们进行编号,以便我可以更轻松地调试它。我跟着这个人的 myLabel
为零并导致我的应用程序崩溃。
我相信我已经正确连接了插座,但也许有人可以提供一份清单让我检查我是否做错了什么。
ViewController
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet var gridView: UICollectionView!
private let reuseIdentifier = "DesignCell"
private let numberOfSections = 1
private let numberOfCircles = 48
override func viewDidLoad() {
super.viewDidLoad()
self.gridView.registerClass(MyCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return numberOfSections
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return numberOfCircles
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyCell
cell.myLabel.text = String(indexPath.item) // myLabel is nil and causes a crash
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
GridLayout(我的自定义布局)
import UIKit
import Darwin
class GridLayout: UICollectionViewLayout {
private let numberOfColumns = 12
private let numberOfRows = 4
private var cache = [UICollectionViewLayoutAttributes]()
private var contentHeight: CGFloat = 0
private var contentWidth: CGFloat {
let insets = collectionView!.contentInset
return CGRectGetWidth(collectionView!.bounds) - insets.left - insets.right
}
override func prepareLayout() {
// some calculation i did for my cells
}
override func collectionViewContentSize() -> CGSize {
return CGSize(width: contentWidth, height: contentHeight)
}
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var layoutAttributes = [UICollectionViewLayoutAttributes]()
for attr in cache {
if CGRectIntersectsRect(attr.frame, rect) {
layoutAttributes.append(attr)
}
}
return layoutAttributes
}
}
我的细胞
import UIKit
class MyCell : UICollectionViewCell {
@IBOutlet weak var myLabel: UILabel!
}
尝试在您的 ViewController class:
的 viewDidLoad() 中删除这一行self.gridView.registerClass(MyCell.self, forCellWithReuseIdentifier: reuseIdentifier)
不需要它,因为您已经在 Storyboard 中创建了 UICollectionView,连接到您的数据源和委托,并且您已经添加了所有必需的方法:
- 节中的项目数量
- cellForItemAtIndexPath