我正在制作一个程序,单个视图控制器有两个集合视图。我已成功填充顶视图控制器,但在添加第二个时,我无法正确填充它。这是我目前的代码。我一直收到错误“无法将类型'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
}
}
答案 0 :(得分:0)
这是我在单个视图控制器中处理两个UICollectionViews的代码。我通过outlet注册了两个集合视图,并为两个单元格制作了自定义UICollectionViewCells
class TeamVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var gameCollectionView: UICollectionView!
@IBOutlet weak var playerCollectionView: UICollectionView!
var teamPlayers = [Player]()
var teamGames = [Game]()
override func viewDidLoad() {
super.viewDidLoad()
gameCollectionView.dataSource = self
gameCollectionView.delegate = self
playerCollectionView.dataSource = self
playerCollectionView.delegate = self
}
//MARK: Collection View Delegate Functions
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if playerCollectionView == self.collectionView {
return teamPlayers.count
}else {
return teamGames.count
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == self.playerCollectionView {
let teamPlayer = teamPlayers[indexPath.row]
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PlayerCell", for: indexPath) as? PlayerCell {
cell.configureCell(player : teamPlayer)
return cell
}else {
return UICollectionViewCell()
}
}else {
let teamGame = teamGames[indexPath.row]
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TeamGameDataCell", for: indexPath) as? TeamGameDataCell {
cell.configureCell(game: teamGame)
return cell
}else {
return UICollectionViewCell()
}
}
}