UICollection视图注册类不起作用



我添加了类控制器UIViewCollectionView

private var friendsCollection: UICollectionView?

带有函数private func addFriendCollectionView(){let layout:UICollectionViewFlowLayout=UICollectionViewFlow layout()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]*断言失败在-[UICollectionView_dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:],/SourceCache/UIKit/UIKit-3347.44.2.2/UICollectionView.m:34542015-11-18 11:44:00.709 SwipeWise[1069:230413]*终止应用程序到期到未捕获的异常"NSInternalConferenceException",原因:"无法将类型为的视图出列:具有的UICollectionElementKindCell标识符firendCell-必须为识别或连接故事板中的原型单元

但我被添加了

friendsCollection?。registerClass(FriendCollectionViewCell.self,forCellWithReuseIdentifier:"friendCell")

我在哪里出错?

您有一个mistypo("firendCell"one_answers"friendCell")

let cell = friendsCollection?.dequeueReusableCellWithReuseIdentifier("firendCell", forIndexPath: indexPath) as! FriendCollectionViewCell

应该是

let cell = friendsCollection?.dequeueReusableCellWithReuseIdentifier("friendCell", forIndexPath: indexPath) as! FriendCollectionViewCell

最新更新