Ios UICollectionViewCell与情节提要
我在故事板的UICollectionView控制器中有一个UICollectionView。UICollectionViewController链接到我的自定义Ios UICollectionViewCell与情节提要,ios,swift,storyboard,uicollectionview,uicollectionviewcell,Ios,Swift,Storyboard,Uicollectionview,Uicollectionviewcell,我在故事板的UICollectionView控制器中有一个UICollectionView。UICollectionViewController链接到我的自定义类MasterViewController:UICollectionViewController、UICollectionViewDataSource、UICollectionViewDelegate,其委托和数据源在情节提要中链接到此类 我在故事板中有一个原型UICollectionViewCell,带有一个标识符“MyCell”,来自
类MasterViewController:UICollectionViewController、UICollectionViewDataSource、UICollectionViewDelegate
,其委托和数据源在情节提要中链接到此类
我在故事板中有一个原型UICollectionViewCell,带有一个标识符“MyCell”,来自我的自定义类单元格:UICollectionViewCell
在cellForItemAtIndexPath
方法中,应用程序在以下行崩溃:let cell:cell=collectionView.dequeueReusableCellWithReuseIdentifier(“MyCell”,forIndexPath:indexPath)作为单元格
我不知道为什么。我还没有实现registerClass:forCellWithReuseIdentifier:方法,故事板的标识符就是“MyCell”,我检查了很多次,委托和数据源链接到了正确的类
当应用程序崩溃时,控制台中不会打印任何内容,只会显示“(lldb)”
这是我的密码:
class MasterViewController: UICollectionViewController,UICollectionViewDataSource,UICollectionViewDelegate {
var objects = [ObjectsEntry]()
@IBOutlet var flowLayout: UICollectionViewFlowLayout!
override func awakeFromNib() {
super.awakeFromNib()
}
override func viewDidLoad() {
super.viewDidLoad()
flowLayout.itemSize = CGSizeMake(collectionView!.bounds.width - 52, 151)
}
// MARK: - Collection View
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return objects.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell:Cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as Cell
return cell
}
我也有同样的问题。帮助了我。我正在这里复制MyCollectionViewController
- 控制器和情节提要中的标识符必须匹配
- 创建自定义
类UICollectionViewCell
- 在情节提要中设置此
UICollectionViewCell
- 不要调用
李>viewDidLoad()
- 不要调用注册表类:forCellWithReuseIdentifier:
- 使用
中的collectionView:layout:sizeForitedIndepath:
设置单元格项大小UICollectionViewDelegateFlowLayout
import UIKit class MyCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { private let reuseIdentifier = "ApplesCell" // MARK: UICollectionViewDataSource override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 1 } override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as MyCollectionViewCell cell.backgroundColor = UIColor.redColor() cell.imageView.image = UIImage(named: "red_apple") return cell }