2016-11-26 1 views
0

Ich habe eine UITableView innerhalb jeder Zelle eines UICollectionView.UITableView innerhalb UICollectionView -> [Fehler] unerwartet gefunden nil beim Auspacken tableView

Ich referenzierte die Tabellenansicht auf eine FeedCell-Klasse, die eine Klasse für jede Zelle in der Sammlungsansicht ist. Allerdings habe ich eine Fehlermeldung,

fatal error: unexpectedly found nil while unwrapping an Optional value 

beim Versuch, das Tableview-Objekt in der FeedCell Klasse zuzugreifen. Ich habe dreifach überprüft, ob ich die Tabellenansicht vom Storyboard korrekt referenziert habe, also gehe ich davon aus, dass es etwas anderes gibt, was dazu führt, dass es null ist, aber ich bin mir nicht sicher, was es ist. Hat jemand eine Ahnung, wie man das beheben kann?

Dies ist mein VC, wo sowohl die Sammlungsansicht als auch die Tabellenansicht liegt.

class HomeViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout, UITableViewDelegate, UITableViewDataSource { 

    @IBOutlet weak var collectionView: UICollectionView! 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     navigationController?.navigationBar.isTranslucent = false 
     collectionView.register(FeedCell.self, forCellWithReuseIdentifier: "FeedCell") 
    } 

    // for feedcell and its collection view 
    func numberOfSections(in collectionView: UICollectionView) -> Int { 
     return 1 
    } 

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FeedCell", for: indexPath) as! FeedCell 
     cell.backgroundColor = backgrounds[indexPath.row] 
     return cell 
    } 

    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { 
     guard let collectionViewCell = cell as? FeedCell else { return } 

     collectionViewCell.setTableViewDataSourceDelegate(dataSourceDelegate: self, forRow: indexPath.row) 
    } 


    func collectionView(_ collectionView: UICollectionView, 
         layout collectionViewLayout: UICollectionViewLayout, 
         sizeForItemAt indexPath: IndexPath) -> CGSize { 

     return CGSize(width: view.frame.width, height: view.frame.height) 
    } 

    // conform to tableview protocol for hometable 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return images[tableView.tag].count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "HomeCell", for: indexPath) 
     cell.imageView?.image = images[tableView.tag][indexPath.item] 
     return cell 
    } 


    override func viewWillAppear(_ animated: Bool) { 
     self.navigationController?.setNavigationBarHidden(true, animated: animated) 
     super.viewWillAppear(animated) 
    } 

    override func viewWillDisappear(_ animated: Bool) { 
     self.navigationController?.setNavigationBarHidden(false, animated: animated) 
     super.viewWillDisappear(animated) 
    } 


} 

Und hier ist eine Zelle der Sammlungsansicht, wo ich auf die Tabellenansicht verweisen.

class FeedCell: BaseCollectionViewCell{ 

    @IBOutlet weak var tableView: UITableView! 
    override func setupViews() { 
     super.setupViews() 
    } 

    func numberOfSections(in collectionView: UICollectionView) -> Int { 
     return 1 
    } 

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

} 


extension FeedCell { 

    func setTableViewDataSourceDelegate 
     <D: UITableViewDataSource & UITableViewDelegate> 
     (dataSourceDelegate: D, forRow row: Int) { 

     # ERROR! 
     tableView.delegate = dataSourceDelegate 
     tableView.dataSource = dataSourceDelegate 
     tableView.tag = row 
     tableView.reloadData() 
    } 

} 
+0

Welche Linie ist Ihr Fehler auf? – TheValyreanGroup

+0

Es ist Zeile "tableView.delegate = dataSourceDelegate". Ich habe einen Kommentar vor der Zeile mit dem Fehler eingefügt. – alpaca

+0

Entfernen Sie einfach diese Zeilen tableView.delegate = dataSourceDelegate tableView.dataSource = dataSourceDelegate und setzen Sie Delegaten aus dem Storyboard und seine Arbeit fein –

Antwort

0

Auf dieser Linie:

(dataSourceDelegate: D, forRow row: Int) 

Sie werfen es als Variable: D

Verwenden Sie stattdessen:

tableView.dataSource = D 
tableView.delegate = D 
+0

Der Fehler ist "schwerwiegender Fehler: unerwartet gefunden Null beim Auspacken eines optionalen Werts f" für das TableView-Objekt. Mit Ihrer Lösung bleibt das TableView-Objekt also weiterhin null – alpaca

Verwandte Themen