2017-08-05 84 views
1

我一直在掙扎幾個小時,因爲我對XCode和Swift比較陌生。自定義UICollectionView數據源和委託

我的故事板中有一個CollectionView,想將其數據源和委託方法鏈接到除ViewController以外的單獨的類,但它不起作用。 任何人都可以幫忙嗎?

override func viewDidLoad() { 
    super.viewDidLoad() 

    // 
    self.card.center = CGPoint(x: self.view.center.x, y: self.view.center.y) 

    self.card.layer.cornerRadius = 5 

    self.card.layer.shadowOpacity = 0.1 

    // 

    self.card2.center = CGPoint(x: self.view.center.x, y: self.view.center.y) 

    self.card2.layer.cornerRadius = 5 

    self.card2.layer.shadowOpacity = 0.1 

    // 

    self.view.bringSubview(toFront: self.card) 

    // HERE IS THE LINK 

    setDS() 

    collectionView.reloadData() 
    // ---------------------- 




} 

private func setDS() { 

    let dataSourceAndDelegate = CollectionViewController() 

    collectionView.dataSource = dataSourceAndDelegate 
    collectionView.delegate = dataSourceAndDelegate 


} 


import UIKit 

private let reuseIdentifier = "Cell" 

class CollectionViewController: UICollectionViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.view.backgroundColor = UIColor.red 

     print("View did load") 


     // Uncomment the following line to preserve selection between presentations 
     // self.clearsSelectionOnViewWillAppear = false 

     // Register cell classes 
     self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) 


     // Do any additional setup after loading the view. 
    } 

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

    /* 
    // MARK: - Navigation 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    // Get the new view controller using [segue destinationViewController]. 
    // Pass the selected object to the new view controller. 
    } 
    */ 

    // MARK: UICollectionViewDataSource 

    override func numberOfSections(in collectionView: UICollectionView) -> Int { 
     // #warning Incomplete implementation, return the number of sections 
     return 1 
    } 


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     // #warning Incomplete implementation, return the number of items 
     return 3 
    } 

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) 

     // Configure the cell 
     cell.backgroundColor = UIColor.blue 

     return cell 
    } 



    // MARK: UICollectionViewDelegate 


    // Uncomment this method to specify if the specified item should be highlighted during tracking 
    override func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool { 
     return true 
    } 



    // Uncomment this method to specify if the specified item should be selected 
    override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool { 
     return true 
    } 



    // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item 
    override func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool { 
     return false 
    } 

    override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool { 
     return false 
    } 

    override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) { 

    } 


} 
+0

使用自定義類,而不是'UICollectionViewController'貫徹UICollectionView數據源協議和UICollectionView委託協議 –

回答

0

請勿使用UICollectionViewController的子類作爲自定義collectionview的數據源和委託。

改爲使用簡單的NSObject類。這樣你只需要實現數據源和委託方法,不必擔心UIViewcontroller的視圖方法(你不需要它們)。

但即使你提供的對象UICollectionViewController它應該工作。它不工作,因爲你沒有保留在你的類ViewController中的對象,它正在自動釋放。 UICollectionView不保留delgeate和數據源以防止保留週期。

let dataSourceAndDelegate = CollectionViewController() 

使dataSourceAndDelegate一個存儲的屬性。另外,你需要在ViewController類中註冊你的單元格(因爲它有你正在使用的集合視圖)。請記住collectionViewUICollectionViewController內的財產與您在ViewController的收藏查看不同。這是一個存儲的財產,因爲UICollectionViewController帶有一個colectionview。

private let reuseIdentifier = "Cell" 

class ViewController: UIViewController { 
    let dataSourceAndDelegate = CollectionViewController() 
    @IBOutlet var collectionView:UICollectionView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.setDS() 
     collectionView.reloadData() 

    } 


    private func setDS() { 
     // Register cell classes 
     self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) 

     collectionView.dataSource = dataSourceAndDelegate 
     collectionView.delegate = dataSourceAndDelegate 

    } 

} 
+2

非常感謝你。 dataSourceAndDelegate不是存儲的屬性是問題。非常感激! – Matt