我添加了类控制器UIViewCollectionView
private var friendsCollection: UICollectionView?
with function private func addFriendCollectionView(){ let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout() layout.sectionInset = UIEdgeInsets(上:20,左:10,下:10,右:10) layout.itemSize = CGSize(宽度:90,高度:120)
let positionRectangle = CGRect(x: 0, y: 100, width: (mainContainerView?.frame.width)!, height: 100)
friendsCollection = UICollectionView(frame: positionRectangle, collectionViewLayout: layout)
friendsCollection?.registerClass(FriendCollectionViewCell.self, forCellWithReuseIdentifier: "friendCell")
let FriendCellNib = UINib(nibName: "FriendCell", bundle:nil)
friendsCollection?.registerNib(FriendCellNib, forCellWithReuseIdentifier: "friendCell")
friendsCollection?.backgroundColor = UIColor.whiteColor()
friendsCollection?.dataSource = self
friendsCollection?.delegate = self
mainContainerView?.addSubview(friendsCollection!)
}
如果代码:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = friendsCollection?.dequeueReusableCellWithReuseIdentifier("firendCell", forIndexPath: indexPath) as! FriendCollectionViewCell
cell.backgroundColor = UIColor.blueColor()
cell.friendName.text = "111111"
return cell
}
返回错误
2015-11-18 11:44:00.708 SwipeWise [1069:230413] *断言失败 in - [UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:],/ SourceCache / UIKit / UIKit-3347.44.2.2 / UICollectionView.m:3454 2015-11-18 11:44:00.709 SwipeWise [1069:230413] * 终止app到期 未被捕获的异常' NSInternalInconsistencyException',原因: '无法将类视图出列:UICollectionElementKindCell with 标识符firendCell - 必须注册一个nib或类 标识符或连接故事板中的原型单元'
但我补充说
friendsCollection?.registerClass(FriendCollectionViewCell.self, forCellWithReuseIdentifier:" friendCell")
我在哪里做错误?
答案 0 :(得分:1)
你有一个mistypo(" firendCell"和" friendCell")
let cell = friendsCollection?.dequeueReusableCellWithReuseIdentifier("firendCell", forIndexPath: indexPath) as! FriendCollectionViewCell
应该是
let cell = friendsCollection?.dequeueReusableCellWithReuseIdentifier("friendCell", forIndexPath: indexPath) as! FriendCollectionViewCell