2017-06-23 47 views
0

我正在嘗試創建一個UICollectionView,並且它在屏幕上未加載。在我的應用程序委託中,我甚至將其作爲輸入屏幕,並且不會僅顯示黑屏。UICollectionView無法在屏幕上加載

class ChatRoom: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UICollectionViewDataSource, UICollectionViewDelegate { 
    // Struct of Objects 
    struct Object { 
     var image: UIImage! 
     var title: String! 
    } 

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

    @IBOutlet weak var collectionView2: UICollectionView! 
    // Instance of a class 
    var secondClass : ChatCell? 

    // 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() 

     // Firebase Data 
     ref = FIRDatabase.database().reference() 

     // CollectionView 
     collectionView2?.delegate = self 
     collectionView2?.dataSource = self 
     collectionView2?.reloadData() 

     // User Logged In 
     checkIfUserLoggedIn() 

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


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

     cell.chatLabel.text = object.title ?? "" 
     cell.chatImage.image = object.image ?? UIImage() 
     cell.layer.borderWidth = 2 
     cell.layer.borderColor = UIColor.white.cgColor 
     cell.layer.masksToBounds = true 
     cell.layer.cornerRadius = 6 

     return cell 
    } 

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

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     let itemWidth = collectionView2.bounds.width 
     let itemHeight = collectionView2.bounds.height 
     return CGSize(width: itemWidth, height: itemHeight) 
    } 

    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 = "Enter only 10 characters" } 

     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

你在哪裏填充對象數組? – Hosny

+0

您還沒有加載項目。 –

+0

是根視圖控制器嗎? – ovo

回答

0

這裏collectionView因爲你不填充objects陣列是dataSourcecollectionView2顯示爲空白。

+0

嘿,我該如何解決這個問題?欣賞幫助 –

+0

您需要添加元素到對象數組,然後重新加載collectionView –

+0

我向數組添加元素...我也重新加載我的viewDidLoad()中的collectionView ... –