2017-01-13 6 views
0

Ich habe Tableview:Nicht wiederverwenden Daten mit ReusableCell von Collection

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

     let cell = tableView.dequeueReusableCell(withIdentifier: "cellTime", for: indexPath) as! TimeViewCell 
     let showHour = self.infoWripper.sorted(by: { $0.hour < $1.hour })[indexPath.row] 

     cell.labelTime.text = showHour.showHour() 

     cell.dataForShow = showHour.showFullTime() 


     return cell 
    } 

Zelle von Tableview Inhalte UICollectionView mit Zellen.

class TimeViewCell: UITableViewCell { 

    @IBOutlet weak var labelTime: UILabel! 

    var dataForShow = [String]() 

    @IBOutlet weak var collectionView: UICollectionView! 

    override func awakeFromNib() { 
     super.awakeFromNib() 

     collectionView.delegate = self 
     collectionView.dataSource = self 

    } 

} 

extension TimeViewCell: UICollectionViewDelegate, UICollectionViewDataSource { 

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

     return dataForShow.count 
    } 


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

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! CustomCollectionCell 

     cell.labelCollection.text = dataForShow[indexPath.row] 

     //heightForCell = collectionView.contentSize.height 

     return cell 
    } 

} 

class CustomCollectionCell: UICollectionViewCell { 

    @IBOutlet weak var labelCollection: UILabel! 

} 

Wenn ich VC mit Tisch öffnen - Ansicht ist in Ordnung, aber wenn ich beginne Scrollen - ich sehe, dass die Daten nicht korrekt (Wiederverwendung), aber ich kann nicht meinen Fehler finden. vor blättern:

enter image description here

nach:

enter image description here

Antwort

1

Sie benötigen die Sammlung Ansicht in der Zelle neu zu laden. Versuchen Sie dies:

class TimeViewCell: UITableViewCell { 
    .... 
    var dataForShow = [String]() { 
     didSet { 
      self.collectionView?.reloadData() 
     } 
    } 
+0

ja! wirklich gut funktioniert, danke! –

Verwandte Themen