2017-06-24 5 views
0

Ich lade zwei Objekte des gleichen Typs in zwei separate Arrays. Das erste Array ist vom Typ [Post], zusammen mit dem zweiten Array. Ich habe zwei Abschnitte in meinem collectionView und ich möchte das erste Array in den ersten Abschnitt und das zweite Array in den zweiten Abschnitt laden. Ich hole die Daten von Firebase und hängt das Objekt an das Array an. In meiner Ich überprüfe, ob der Abschnitt gleich eins ist, laden Sie den ersten und wenn der Abschnitt gleich zwei ist, laden Sie den zweiten. Das funktioniert gut, aber das Problem ist, dass es nur die korrekte Menge an Zellen in jedem Abschnitt lädt, aber nicht die richtigen Informationen lädt. Es lädt nur die zuvor abgerufenen Daten aus dem ersten Array. Wie komme ich hier zurecht? Danke ...Swift: Wägezelle im richtigen Abschnitt von UICollectionView?

class ProfileController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

    var name: String? 

    var posts = [Post]() 
    var repost = [Post]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     collectionView?.register(PostCell.self, forCellWithReuseIdentifier: reuseIdentifier) 
     collectionView?.register(ProfileHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerIdentifier) 
     collectionView?.register(RepostedHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: boostedHeaderIdentifier) 

     self.posts.removeAll() 
     self.repost.removeAll() 

     handleFetchUserPosts { self.collectionView?.reloadData() } 
     handleFetchUserReposts { self.collectionView?.reloadData() } 
    } 

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

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     if section == 0 { 
      return posts.count 
     } else { 
      return reposts.count 
     } 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { 
     if section == 0 { 
      return CGSize(width: view.frame.width, height: 180) 
     } else { 
      return CGSize(width: view.frame.width, height: 30) 
     } 
    } 

    override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 

     if indexPath.section == 0 { 
      let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerIdentifier, for: indexPath) as! ProfileHeader 
      header.profileController = self 
      return header 
     } else { 
      let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: boostedHeaderIdentifier, for: indexPath) as! RepostedHeader 
      return header 
     } 

    } 

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

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

     cell.profileController = self 

     let posts = self.posts[indexPath.item] 
     let reposts = self.reposts[indexPath.item] 

     if self.posts != [] { 
      cell.post = posts 
     } 

     if self.posts != [] { 
      cell.post = reposts 
     } 

     return cell 
    } 
} 

Antwort

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

    cell.profileController = self 

    if indexPath.section == 0 { 
     let posts = self.posts[indexPath.item] 
     if self.posts != [] { 
      cell.post = posts 
     } 
    } 

    if indexPath.section == 1 { 
     let reposts = self.reposts[indexPath.item] 
     if self.posts != [] { 
      cell.post = reposts 
     } 
    } 

    return cell 
} 

Ich denke, das Ihr Problem zu lösen, wenn nicht diese, eigentlich ist dies Art von gleichem Code, aber wenn Sie für Ihre Objekte mit mehreren Zellen haben diese kommen praktisch

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    var cell = PostCell() 

    if indexPath.section == 0 { 
     cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PostCell 
     cell.profileController = self 
     let posts = self.posts[indexPath.item] 
     if self.posts != [] { 
      cell.post = posts 
     } 
    } 

    if indexPath.section == 1 { 
     cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PostCell 
     let reposts = self.reposts[indexPath.item] 
     if self.posts != [] { 
      cell.post = reposts 
     } 
    } 

    cell.profileController = self 

    return cell 
} 

hoffe das hilft

+0

Absolut Spot auf. Ich danke dir sehr. Korrekte Antwort. –

Verwandte Themen