使用 UICollectionView Yalantis/Koloda 时初始卡片相同

Posted

技术标签:

【中文标题】使用 UICollectionView Yalantis/Koloda 时初始卡片相同【英文标题】:Initial cards are the same when using UICollectionView Yalantis/Koloda 【发布时间】:2017-11-22 00:29:10 【问题描述】:

我正在使用这个库,并且能够将 Firebase 中的数据显示到卡片上。但是初始卡(默认 3 个)都是一样的。然后,一旦我滑动它们,我就可以看到下一个结果。似乎只有当我在卡片上显示 CollectionView 而不仅仅是 UIView 时才会出现这种情况。

这是我获取数据的方式:

var person: Person!

func fetchUser() 

    let databaseRef = Database.database().reference(withPath: "users")
    databaseRef.observeSingleEvent(of: DataEventType.value, with:  (snapshot) in
        for child in snapshot.children 
            guard let snapshot = child as? DataSnapshot else  continue 

            let person = Person(from: snapshot)
            self.person = person
            PersonManager.shared.addPerson(person)
        
        self.kolodaView.reloadData()

    )

这是我将数据分配给单元格的方式:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCell
    let person = PersonManager.shared.persons[indexPath.row]
    cell.nameLabel.text = person.firstName
    return cell

PersonManager 是处理Person 对象并具有append()removeAll() 函数的单例。

这是我的结果:

有没有人让它与 tableView 或 collectionView 一起正常工作?

【问题讨论】:

【参考方案1】:

确保 koloda(_ koloda: KolodaView, viewForCardAt index: Int) 中的索引有效。您似乎对所有图像都使用了一个索引。

【讨论】:

以上是关于使用 UICollectionView Yalantis/Koloda 时初始卡片相同的主要内容,如果未能解决你的问题,请参考以下文章

UICollectionView介绍使用

使用 NSFetchResultsController 时 UICollectionView 滚动不流畅

如何防止使用 UICollectionView 滚动 UIScrollView

iOS 使用 PullToRefreshView 使 UICollectionView 可滚动

如何使用 UICollectionView 创建 3 列

如何使用 swift 重置 UICollectionView 滚动位置?