Swift:在UICollectionView的正确部分加载单元格?

时间:2017-06-24 18:35:22

标签: swift swift3 uicollectionview uicollectionviewcell

我将两个相同类型的对象加载到两个单独的数组中。第一个数组是[Post]类型,还有第二个数组。我在collectionView中有两个部分,我想将第一个数组加载到第一个部分,第二个数组加载到第二个部分。我从firebase获取数据并将对象附加到数组中。在我的numberOfItemsInSection我正在检查该部分是否等于1,加载第一个,如果该部分等于2,则加载第二个。这很好但问题是它只在每个部分加载正确数量的单元格但没有加载正确的信息。它只是从第一个数组加载以前获取的数据。我该如何解决这个问题?谢谢......

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
    }
}

1 个答案:

答案 0 :(得分: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
}

我认为如果不使用它,这将解决您的问题,实际上这是一种相同的代码,但如果您的对象有多个单元格,这将会很方便

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
}

希望这会有所帮助