2017-08-05 1 views
1

Ich habe seit Stunden damit zu kämpfen, wie ich relativ neu zu XCode und Swift bin.Benutzerdefinierte UICollectionView-Datenquelle und Delegierter

Ich habe eine CollectionView in meinem Storyboard und möchte seine Datenquelle und delegieren Methoden zu einer separaten Klasse als meine ViewController verknüpfen, aber es funktioniert nicht. Kann jemand helfen?

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

Verwendung eine benutzerdefinierte Klasse statt 'UICollectionViewController' und implementieren die UICollectionView Datasource-Protokoll und UICollectionView Delegate Protokoll –

Antwort

0

Verwenden Sie Unterklasse von UICollectionViewController als Datenquelle und Delegierter des benutzerdefinierten Kollektion nicht.

Verwenden Sie stattdessen einfache NSObject-Klasse. Auf diese Weise müssten Sie nur die Datenquellen- und Delegate-Methoden implementieren und müssen sich nicht um die View-Methoden von UIViewcontroller kümmern (Sie brauchen sie sowieso nicht).

Aber selbst wenn Sie Objekt UICollectionViewController liefern, sollte es funktionieren. Es funktioniert nicht, weil Sie das Objekt in Ihrer Klasse ViewController nicht beibehalten haben und es wird automatisch freigegeben. UICollectionView behält Delgeate und Datenquelle nicht bei, um Retain-Zyklen zu verhindern.

let dataSourceAndDelegate = CollectionViewController() 

Machen Sie dataSourceAndDelegate eine gespeicherte Eigenschaft.

Außerdem müssten Sie Ihre Zelle in der ViewController-Klasse registrieren (weil sie die Auflistungsansicht hat, mit der Sie arbeiten). Denken Sie daran, collectionView Eigenschaft innerhalb UICollectionViewController ist nicht das gleiche wie Ihre SammlungView in ViewController. Es ist eine gespeicherte Eigenschaft, weil UICollectionViewController mit einem colectionview kommt.

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

Ihnen sehr danken. Das Problem bestand darin, dass dataSourceAndDelegate keine gespeicherte Eigenschaft war. Sehr geschätzt! – Matt