2017-01-12 3 views
0

Я делаю программу, у одного контроллера просмотра есть два вида коллекции. Я успешно заполнил контроллер верхнего уровня, но при добавлении второго я не могу правильно его заполнить. это мой текущий код. Я продолжаю получать ошибку "Не удалось бросить значение типа 'OperationDepth.messageCollectionViewCell' (0x10a4e1e98) к 'OperationDepth.matchCollectionViewCell' (0x10a4e1c98). (lldb)"CollectionViewCell не создает

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

Вы используете раскадровки? – Mochi

+0

Да, я использую раскадровки @Mochi –

+0

В раскадровках попробуйте изменить «ячейку» на более уникальный идентификатор, такой как «MatchCell» и «MessageCell». Сделайте то же самое с кодом. – Mochi

ответ

0

Вот мой код, чтобы обрабатывать два UICollectionViews в одном контроллере. Я зарегистрировал оба вида просмотра через выходы и сделал пользовательские 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() 
      } 
     } 
    } 
Смежные вопросы