【发布时间】:2017-01-12 23:08:18
【问题描述】:
我正在制作一个具有两个集合视图的单个视图控制器的程序。我已经成功填充了顶视图控制器,但是当添加第二个时,我无法正确填充它。这是我当前的代码。我不断收到错误消息“无法将'OperationDepth.messageCollectionViewCell'(0x10a4e1e98)类型的值转换为'OperationDepth.matchCollectionViewCell'(0x10a4e1c98)。 (lldb) "
class ChatViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var matchImage: [UIImage] = [UIImage(named: "image1.jpg")!,
UIImage(named: "image2.jpg")!,
UIImage(named: "image3.jpg")!,
UIImage(named: "image4.jpg")!,
UIImage(named: "image5.jpg")!,
UIImage(named: "image6.jpg")!]
var matches = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48"]
// MARK: - UICollectionViewDataSource protocol
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return matchImage.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
// get a reference to our storyboard cell
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MatchCell", forIndexPath: indexPath) as! matchCollectionViewCell
let image = matchImage[indexPath.row]
cell.matchImage.image = image
cell.layer.masksToBounds = true;
cell.layer.cornerRadius = 35
return cell
}
class MessageViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var matches = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48"]
// MARK: - UICollectionViewDataSource protocol
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return matches.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
// get a reference to our storyboard cell
let cellId = collectionView.dequeueReusableCellWithReuseIdentifier("MessageCell", forIndexPath: indexPath) as! messageCollectionViewCell
// Use the outlet in our custom class to get a reference to the UILabel in the cell
// cell.myLabel.text = self.items[indexPath.item]
//cell.backgroundColor = UIColor(red: 102/256, green: 255/256, blue: 255/256, alpha: 0.66)
//cell.layer.borderWidth = 1
return cellId
}
}
【问题讨论】:
-
你在使用 Storyboard 吗?
-
是的,我正在使用情节提要@Mochi
-
在情节提要中,尝试将“cell”更改为更独特的标识符,例如“MatchCell”和“MessageCell”。对您的代码执行相同操作。
-
@Mochi 我已经按照您的要求进行了更改。我没有收到此错误“由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无法使一种视图出列:带有标识符MatchCell的UICollectionElementKindCell - 必须为标识符注册一个笔尖或一个类或连接一个原型单元格故事板'”。还对上面的代码进行了修改
-
您是否在情节提要中注册了两个自定义单元格,它们各自的重用标识符“MatchCell”和“MessageCell”以及它们各自的自定义类messageCollectionViewCell和matchCollectionViewCell
标签: ios swift collectionview