2017-11-18 1 views
0

Ich habe eine Tabellenansicht mit zwei Zellen und zwei eingebetteten Collectionviews in diesen Zellen. Meine App stürzt beim Versuch, die zweite Zelle zu duplizieren, ab. Ich habe das Ash-Furrow-Tutorial als Grundlage für dieses Projekt verwendet, das ich hier verlinken werde. Tutorial linkWie erstellt man mehrere Datenquellen für die Sammlungsansicht?

Wie kann ich dieses Problem beheben? Hier ist ein Beispielcode.

Ich habe zwei verschiedene Zellen mit jedem von ihnen mit diesem Code ihre eigene Klasse mit

class TableViewCell: UITableViewCell { 

    @IBOutlet weak var collectionView: UICollectionView! 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
    } 

    override func setSelected(_ selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 

    func setCollectionViewDataSourceDelegate <D: UICollectionViewDataSource & UICollectionViewDelegate> (dataSourceDelegate: D, forRow row: Int) { 
     collectionView.delegate = dataSourceDelegate 
     collectionView.dataSource = dataSourceDelegate 
     collectionView.tag = row 
     collectionView.reloadData() 
    } 
} 

Dies ist der Rest des Codes zeigt, wie ich meine Zelle aus der Warteschlange entfernt wird

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
     if indexPath.section == 1 { 
      guard let tableViewCell = cell as? TableViewCell else { 
       return 
      } 

      tableViewCell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, forRow: indexPath.row) 
     } 

     if indexPath.section == 2 { 
      guard let tableViewCell2 = cell as? TableViewCell2 else { 
       return 
      } 
      tableViewCell2.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, forRow: indexPath.row) 
     } 


     // we store the offset from here into the dictionary we created above 
     //tableViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0 
    } 

    func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
     guard let tableViewCell = cell as? TableViewCell else { return } 

     storedOffsets[indexPath.row] = tableViewCell.collectionViewOffset 
    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     print(indexPath) 
    } 
} 

// collectionView inside tableview cell 

extension TabViewController1: UICollectionViewDelegate, UICollectionViewDataSource { 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return model[collectionView.tag].count 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 


     if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as? CollectionCell1 { 
      cell.backgroundColor = UIColor.gray 
      return cell 
     } 

     if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell2", for: indexPath) as? CollectionCell2 { 
      cell.backgroundColor = UIColor.purple 
      return cell 
     } 

     return UICollectionViewCell() 
    } 

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     print("Collection view at row \(collectionView.tag) selected index path \(indexPath)") 
    } 
} 

Jede Hilfe werden sehr geschätzt!

EDIT: Das ist die ganze View-Controller-Klasse

class TabViewController1: UIViewController, UITableViewDelegate { 

    @IBOutlet private var tableView: UITableView! 

    var model: [[UIColor]] = [] 

    // Here we create a new dictionary to store the offests, corresponding to their rows. 
    var storedOffsets = [Int: CGFloat]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     print("tab1") 

     model = generateRandomData() 
     tableView.delegate = self 
    } 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 
     print("tab 1 viewWillAppear") 
    } 

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

    func generateRandomData() -> [[UIColor]] { 
     let numberOfRows = 20 
     let numberOfItemsPerRow = 15 

     return (0..<numberOfRows).map { _ in 
      return (0..<numberOfItemsPerRow).map { _ in UIColor.randomColor() } 
     } 
    } 
} 

extension TabViewController1: UITableViewDataSource { 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return 5 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if section == 3 { 
      return 1 
     } 
     if section == 4 { 
      return 6 
     } else { 
      return 1 
     } 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     if indexPath.section == 0 { 
      let cell = tableView.dequeueReusableCell(withIdentifier: "featuredPicture", for: indexPath) as! FeaturedVideoTableViewCell 
      return cell 
     } 

     if indexPath.section == 1 { 
      let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell 
      return cell 
     } 


     if indexPath.section == 2 { 
      let cell = tableView.dequeueReusableCell(withIdentifier: "Cell2", for: indexPath) as! TableViewCell2 
      return cell 
     } 

     if indexPath.section == 3 { 

      let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell", for: indexPath) as! HeaderTableViewCell 
      return cell 
     } 

     if indexPath.section == 4 { 

      let cell = tableView.dequeueReusableCell(withIdentifier: "categoryCell", for: indexPath) as! CategoryTableViewCell 
      return cell 
     } 

     return UITableViewCell() 

    } 

    // titleForHeaderInSection doesnt get called because I implemented the bottom method which is `viewForHeaderInSection`. 
    // I am trying to figure out if we can use both at the same time 
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 

     if section == 0 { 
      return "Featured" 
     } 

     if section == 1 { 
      return "Cool Stuff" 
     } 

     if section == 2 { 
      return "Awesome Stuff" 
     } 

     if section == 3 { 
      return "nothing" 
     } 

     if section == 4 { 
      return "Categories" 
     } 

     return String() 
    } 

    // viewForHeaderInSection 
// func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
//  if section == 3 { 
//   let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell") as! HeaderTableViewCell 
//   return cell 
//  } 
//  return UIView() 
// } 

// func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
//  if section == 3 { 
//   return 50 
//  } 
//  return CGFloat() 
// } 

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
     if indexPath.section == 1 { 
      guard let tableViewCell = cell as? TableViewCell else { 
       return 
      } 

      tableViewCell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, forRow: indexPath.row) 
     } 

     if indexPath.section == 2 { 
      guard let tableViewCell2 = cell as? TableViewCell2 else { 
       return 
      } 
      tableViewCell2.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, forRow: indexPath.row) 
     } 


     // we store the offset from here into the dictionary we created above 
     //tableViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0 
    } 

    func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
     guard let tableViewCell = cell as? TableViewCell else { return } 

     storedOffsets[indexPath.row] = tableViewCell.collectionViewOffset 
    } 

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     if indexPath.section == 0 { 
      return 350 
     } 

     if indexPath.section == 1 { 
      return 130 
     } 

     if indexPath.section == 2 { 
      return 200 
     } 

     if indexPath.section == 3 { 
      return 50 
     } 

     if indexPath.section == 4 { 
      return 220 
     } 

     return CGFloat() 
    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     print(indexPath) 
    } 
} 

// collectionView inside tableview cell 

extension TabViewController1: UICollectionViewDelegate, UICollectionViewDataSource { 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return model[collectionView.tag].count 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as? CollectionCell1 { 
      cell.backgroundColor = UIColor.gray 
      return cell 
     } 

     if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell2", for: indexPath) as? CollectionCell2 { 
      cell.backgroundColor = UIColor.purple 
      return cell 
     } 

     return UICollectionViewCell() 
    } 


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     print("Collection view at row \(collectionView.tag) selected index path \(indexPath)") 
    } 
} 
+0

Haben Sie eine Schnittansicht? – ivarun

+0

@ivarun im Moment habe ich Abschnitte. Ich füge die ganze Klasse unter EDIT hinzu, damit du es sehen kannst. Danke für die Hilfe – user7097242

Antwort

0

Ohne zu sehen, wo Sie Ihre Zellklassen sind Registrierung, ist es schwer, sicher zu sagen, aber mein Verdacht ist, dass Sie immer wieder etwas anderes als ein TableViewCell2 (oder wo auch immer es tatsächlich abstürzt) in den Methoden -cellForRow ... oder -cellForItemAt .... All Ihre Kraftentfaltung (!) Verursacht wahrscheinlich ein Problem. Setzen Sie einen Haltepunkt direkt vor dem Absturz und überprüfen Sie, was Sie tatsächlich von Ihrem Aufruf -dequeueReusableCell zurückbekommen.

+0

Ich habe die ganze Klasse unter Bearbeiten oben hinzugefügt? Was möchtest du sonst noch sehen? Darf ich Ihnen dieses Dummy-Projekt schicken, um es sich anzuschauen? – user7097242

+0

bist du noch hier ??? – user7097242

Verwandte Themen