2017-03-09 95 views
0

我有一個UIViewController,並嵌入我有一個UICollectionView,我也有叫ChatCell一個子類我得到這個錯誤,這個類不是密鑰集合視圖的密鑰值編碼。 ,

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! 

//我的錯誤是^ I更改了名字,我沒有更新的故事板出口!確保你有正確的插座,否則它們將不被xcode識別。

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

它的工作!謝謝我現在明白我做錯了什麼。 –

相關問題