2016-08-25 39 views
0

我在storyboard.rootview屏幕中有兩個屏幕,就像啓動畫面,另一個是使用帶標題的集合視圖創建的主屏幕。無法查看單擊菜單按鈕後創建的集合中的單元格

在標題我有一個UIButton通過點擊,我應該能夠顯示我的應用程序,這也是一個集合視圖的菜單選項。由於我對iOS編程相對較新,因此我遵循a video in youtube

問題是根據視頻我能夠創建集合視圖作爲子視圖,但在實現collecton視圖func並運行應用程序後無法查看單元格我配置

viewcontroller.swift的代碼是這樣

import UIKit 

class ViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate{ 
    var tableLabel:[String]=[""] 
    var tableImage: [String]=[""] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

    } 

    let settingLauncher=SettingLauncher() 

    @IBAction func menuButtonTaped(sender: UIButton) { 

     settingLauncher.showSettings() 

    } 

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

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell:CellCollectionViewCell=collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as! CellCollectionViewCell 
     cell.layer.cornerRadius=10 

     cell.labelOne.text=tableLabel[indexPath.row] 
     cell.imageOne.image=UIImage(named:tableImage[indexPath.row]) 
     cell.imageOne.layer.cornerRadius=41 
     cell.imageOne.layer.masksToBounds=true 
     return cell 
    } 
    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView 
    { 

     let header = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "HomeHeader", forIndexPath: indexPath) as! CellCollectionReusableView 

     return header 
    } 






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


} 

settingLauncher.swift文件是這樣的

import UIKit 

class SettingLauncher:NSObject,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{ 

    override init() { 
     super.init() 

     collectionView.dataSource=self 
     collectionView.delegate=self 

     collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId) 


    } 

    let blackView=UIView() 


    var collectionView:UICollectionView={ 
     let layout = UICollectionViewLayout() 
     let cv=UICollectionView(frame: .zero, collectionViewLayout: layout) 
     cv.backgroundColor=UIColor.blueColor() 
     return cv 
    }() 

    let cellId="cellId" 


    func showSettings() { 

    if let window = UIApplication.sharedApplication().keyWindow{ 
     blackView.backgroundColor=UIColor(white: 0,alpha: 0.5) 
     blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dismissShade))) 
     window.addSubview(blackView) 
     window.addSubview(collectionView) 
     let height:CGFloat=200 
     let y:CGFloat=window.frame.height-height 
     collectionView.frame=CGRectMake(0, window.frame.height, window.frame.width, height) 
     blackView.frame=window.frame 
     UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .CurveEaseInOut, animations: { 

      self.collectionView.frame=CGRectMake(0, y, self.collectionView.frame.width, self.collectionView.frame.height) 
      }, completion: nil) 

    } 
    } 

    func dismissShade(){ 
     UIView.animateWithDuration(0.5){ 

      self.blackView.alpha=0 
      if let window = UIApplication.sharedApplication().keyWindow{ 
      self.collectionView.frame=CGRectMake(0, window.frame.height, self.collectionView.frame.width, self.collectionView.frame.height) 
      } 


     } 

    } 



    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 3 
    } 



    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath) 

     cell.backgroundColor=UIColor.redColor() 

     return cell 

    } 

} 

應用構建成功,未出現任何錯誤,但我無法看到它獲取點擊菜單

menu

之前點擊菜單按鈕

這裏截屏後創建的集合中的細胞這裏點擊屏幕菜單上後

menu

+0

顯示崩潰報告 –

+0

應用程序沒有崩潰,無法查看我的settingLauncher類中創建的colletionView細胞 – Padamavthi

+0

然後按照DDB答案 –

回答

0

你應該設置你的拍攝collectionViewdataSourcedelegate到你的ViewController類中的viewController(就像你爲SettingLauncher類所做的那樣)。

+0

在你的showSettings()你可以嘗試調用self.collectionView.reloadData()? – ddb

+0

但我試過self.collectionView,reloadData()但結果是一樣的沒有細胞在我的收藏查看 – Padamavthi

+0

分享一些屏幕截圖的問題 – ddb

相關問題