2017-03-09 8 views
0

私はのUIViewControllerを持っている、と私はUICollectionViewを持っている組み込み、私はまた、サブクラスは私のエラーは^私は名前を変更したと私はストーリーボードにコンセントを更新しませんでした// このエラーが発生しました。このクラスは、キーcollectionViewのキーコードに準拠していません。 、

ChatCell Subclass: 
class ChatCell: UICollectionViewCell { 

    @IBOutlet weak var chatLabel: UILabel! 
    @IBOutlet weak var chatImage: UIImageView! 

} 

class ChatRoom: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UICollectionViewDataSource, UICollectionViewDelegate { 



    @IBOutlet weak var collectionView2: UICollectionView! 

ChatCell

呼ばれています!正しいアウトレットを持っていることを確認してください。そうでないとxcodeで認識されません。あなたはコンセントへの接続を指定した

ストーリーボード(またはXIB)で
var secondClass : ChatCell? 

    struct Object { 
     var image: UIImage! 
     var title: String! 
    } 

    // Properties 
    var object: Object? 
    var objects: [Object] = [] 
    var picker: UIImagePickerController! 


    // Do any additional setup after loading the view, typically from a nib. 


    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     picker = UIImagePickerController() 
     picker?.allowsEditing = false 
     picker?.delegate = self 
     picker?.sourceType = .photoLibrary 
     // NavigationBar Characteristics 
    } 

    override func viewDidAppear(_ animated: Bool) { 
     self.navigationController?.isNavigationBarHidden = false 
     self.navigationController?.navigationBar.barTintColor = UIColor.black 
     self.navigationController?.navigationBar.isTranslucent = false 
     // self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(handleLogout)) 
     self.navigationItem.leftBarButtonItem?.tintColor = UIColor.green 
     self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "+", style: .plain, target: self, action: #selector(didSelectCreateButton)) 
     self.navigationItem.rightBarButtonItem?.tintColor = UIColor.green 

    } 


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as UICollectionViewCell 
     let object = objects[indexPath.row] 

     secondClass?.chatLabel.text = object.title ?? "" 
     secondClass?.chatImage.image = object.image ?? UIImage() 


     return cell 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return objects.count 

    } 

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 

     switch info[UIImagePickerControllerOriginalImage] as? UIImage { 
     case let .some(image): 
      object?.image = image 
     default: 
      break 
     } 

     picker.dismiss(animated: true) { 
      self.showCellTitleAlert() 
     } 
    } 

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { 

     object = nil 

     dismiss(animated: true) { 
      self.collectionView2!.reloadData() 
     } 
    } 

    // Alerts 

    func showCellTitleAlert() { 

     let alert = UIAlertController(title: "Cell Title", message: nil, preferredStyle: .alert) 

     alert.addTextField { $0.placeholder = "Title" } 
     alert.addAction(UIAlertAction(title: "Cancel", style: .cancel) { _ in 
      self.object = nil 
     }) 
     alert.addAction(UIAlertAction(title: "Save", style: .default) { _ in 
      self.object?.title = (alert.textFields?.first.flatMap { $0.text })! 
      self.object.flatMap { self.objects.append($0) } 
      self.collectionView2?.reloadData() 
     }) 

     present(alert, animated: true, completion: nil) 
    } 

    // Create new Cell 
    ////////?////////////// 
    @IBAction func didSelectCreateButton() { 

     object = Object() 

     present(picker, animated: true, completion: nil) 
    } 

} 
+0

すべての '@ IBOutlet'がstoryboard/xibに正しく配線されていることを確認してください。その後、派生したデータを消去し、プロジェクトをクリーンアップします。それはあなたの問題を解決するはずです。 – byJeevan

答えて

1

collectionViewと呼ばれるが、あなたのコードの中で、あなたはcollectionView2それを呼び出します。

不正な接続を削除し、現在の変数名を指すように再接続します。

+0

助けてくれてありがとう、私はなぜそれが悪い接続ですか? –

+0

それは働いた!ありがとう、私は間違ったことを今理解しています。 –