我的带有自定义单元格的 CollectionView 不断使程序崩溃,即使它最初 compiles/runs
My CollectionView with custom cells keeps crashing the program, even tho it initially compiles/runs
我正在尝试创建一个 UICollectionView,其中的单元格中包含 textLabel,程序可以编译、运行,但是当我尝试使用此 table 转到屏幕时会崩溃。
它有一个自定义单元格class,名为GridViewCell,只有一个字段,自定义class继承自UICollectionViewCell。 ViewController 的代码是:
import Foundation
import UIKit
class GridViewController : UICollectionViewController{
@IBOutlet var gridView: UICollectionView! //this is the ViewController
var someArray1 : [String] = []
var someArray2 : [String] = []
var totalSize: Int {
return entireArray.count
}
var entireArray: [[String]] {
return combine(someArray1, someArray2) //combine takes the arrays and makes a 2D array with both of them in it
}
let subArraySize = 4
override func viewDidLoad(){
super.viewDidLoad()
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "gridCell")
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return totalSize * subArraySize
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> GridViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath as IndexPath) as? GridViewCell{
cell.textLabel.text = "ddd" //sample text just to test it
cell.backgroundColor = UIColor.gray
return cell
}
else {
DLog("error")
return GridViewCell()
}
}
}
我在调试器中得到的错误信息是:
"Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the cell returned from -collectionView:cellForItemAtIndexPath: does not have a reuseIdentifier - cells must be retrieved by calling - dequeueReusableCellWithReuseIdentifier:forIndexPath:'"
在我的界面生成器中,我已将重用标识符指定为 "gridCell",它的 class 是 GridViewCell。
我该如何纠正?
试试这个:(如果您没有使用 Interface Builder)
override func viewDidLoad(){
super.viewDidLoad()
self.collectionView!.register(GridViewCell.self, forCellWithReuseIdentifier: "gridCell")
}
如果您从情节提要中加载单元格,则不需要注册单元格
override func viewDidLoad(){
super.viewDidLoad()
}
如果您从 nib/xib 加载一个单元格,那么您可以这样做:
self.collectionView!.register(UINib(nibName: "GridViewCell", bundle: nil), forCellReuseIdentifier: "GridViewCell")
希望对您有所帮助。
尝试将 cellForItemAt 更改为以下内容
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath as IndexPath) as? GridViewCell{
cell.textLabel.text = "ddd" //sample text just to test it
cell.backgroundColor = UIColor.gray
return cell
}
我根据Xcode7.3对你的代码做了一些小改动,希望对你有所帮助。
class GridViewController: UICollectionViewController {
@IBOutlet var gridView: UICollectionView!
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("gridCell", forIndexPath: indexPath)
cell.backgroundColor = UIColor.grayColor()
return cell }
}
我已经测试过了,效果很好。
在我的例子中,我调用的是 "collectionView.register(, forCellWithReuseIdentifier: <#T##String#>)" 而不是 collectionView.register(, forCellWithReuseIdentifier: <#T##String#>)
我正在尝试创建一个 UICollectionView,其中的单元格中包含 textLabel,程序可以编译、运行,但是当我尝试使用此 table 转到屏幕时会崩溃。
它有一个自定义单元格class,名为GridViewCell,只有一个字段,自定义class继承自UICollectionViewCell。 ViewController 的代码是:
import Foundation
import UIKit
class GridViewController : UICollectionViewController{
@IBOutlet var gridView: UICollectionView! //this is the ViewController
var someArray1 : [String] = []
var someArray2 : [String] = []
var totalSize: Int {
return entireArray.count
}
var entireArray: [[String]] {
return combine(someArray1, someArray2) //combine takes the arrays and makes a 2D array with both of them in it
}
let subArraySize = 4
override func viewDidLoad(){
super.viewDidLoad()
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "gridCell")
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return totalSize * subArraySize
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> GridViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath as IndexPath) as? GridViewCell{
cell.textLabel.text = "ddd" //sample text just to test it
cell.backgroundColor = UIColor.gray
return cell
}
else {
DLog("error")
return GridViewCell()
}
}
}
我在调试器中得到的错误信息是:
"Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the cell returned from -collectionView:cellForItemAtIndexPath: does not have a reuseIdentifier - cells must be retrieved by calling - dequeueReusableCellWithReuseIdentifier:forIndexPath:'"
在我的界面生成器中,我已将重用标识符指定为 "gridCell",它的 class 是 GridViewCell。
我该如何纠正?
试试这个:(如果您没有使用 Interface Builder)
override func viewDidLoad(){
super.viewDidLoad()
self.collectionView!.register(GridViewCell.self, forCellWithReuseIdentifier: "gridCell")
}
如果您从情节提要中加载单元格,则不需要注册单元格
override func viewDidLoad(){
super.viewDidLoad()
}
如果您从 nib/xib 加载一个单元格,那么您可以这样做:
self.collectionView!.register(UINib(nibName: "GridViewCell", bundle: nil), forCellReuseIdentifier: "GridViewCell")
希望对您有所帮助。
尝试将 cellForItemAt 更改为以下内容
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath as IndexPath) as? GridViewCell{
cell.textLabel.text = "ddd" //sample text just to test it
cell.backgroundColor = UIColor.gray
return cell
}
我根据Xcode7.3对你的代码做了一些小改动,希望对你有所帮助。
class GridViewController: UICollectionViewController {
@IBOutlet var gridView: UICollectionView!
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("gridCell", forIndexPath: indexPath)
cell.backgroundColor = UIColor.grayColor()
return cell }
}
我已经测试过了,效果很好。
在我的例子中,我调用的是 "collectionView.register(, forCellWithReuseIdentifier: <#T##String#>)" 而不是 collectionView.register(, forCellWithReuseIdentifier: <#T##String#>)