gpt4 book ai didi

ios - 单例类不使用 Swift 更新 UICollectionViewCell 中的数据

转载 作者:行者123 更新时间:2023-11-28 07:58:06 25 4
gpt4 key购买 nike

我不确定这样说是否正确,但我一直在尝试将数据获取到 ColectionViewCell,但它似乎并没有显示所有数据。这个问题来自 Cards are not displaying different data. Koloda/Yalantis .该框架似乎需要一种不同的方法来将数据应用于索引,但我不确定如何实现这一点。

这是我的类(class)的样子:

import SwiftyJSON

class Person {

var firstName: String?


init(json: JSON) {
self.firstName = json["first_name"].stringValue
}
}

这是我管理 Person 类的单例:

class PersonManager {

static let shared = PersonManager()
private init() {}

var persons = [Person]()

func removeAll() {
persons.removeAll()
}

func addPerson(_ person: Person) {
persons.append(person)
}
}

下面是我如何尝试在获取初始化附加数据后调用数据:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)

let person = PersonManager.shared.persons[kolodaView.currentCardIndex]
//Returns the first persons first name as it is supposed to
cell.textLabel?.text = person.firstName
}

数据的存在是因为它计算了我数据库中的人数。但它会继续返回所有卡片的第一个人。

更新:

我使用 Firebase 获取用户数据,然后像这样附加它:

func fetchUser() {
let databaseRef = Database.database().reference(withPath: "users")
databaseRef.observe( .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)

let person = PersonManager.shared.persons[indexPath.row]
//Returns the first persons first name as it is supposed to
cell.textLabel?.text = person.firstName
}

问题是您的变量 kolodaView.currentCardIndex 始终是 0,它永远不会显示其他结果,只会显示集合中的第一个结果。

indexPath.row 是用来控制你需要的这个索引的。

关于ios - 单例类不使用 Swift 更新 UICollectionViewCell 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47383770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com