gpt4 book ai didi

ios - 从核心数据获取的代码未在表格 View 单元格标签中正确显示

转载 作者:行者123 更新时间:2023-11-29 05:40:51 26 4
gpt4 key购买 nike

我正在从核心数据中获取并将其打印在标签上。问题是标签上打印的内容有很多流失的内容。正如您在下面的照片中看到的。在每个 Collection View 单元格中,我希望它打印数组的 1 个元素。因此,如果数组有 [vanessa,taylor,bastista]。 Collection View 单元格应打印 Vanessa。

        var people: [Jessica] = []
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = newCollection.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! CustomeCell
cell.backgroundColor = .white
cell.layer.cornerRadius = 5
cell.layer.shadowOpacity = 3
cell.textLabel.text = people[indexPath.row].playName
return cell
}

更多方法

      override func viewWillAppear(_ animated: Bool) {
fetchData()
}

func fetchData() {

do {
items = try context.fetch(Jessica.fetchRequest())

DispatchQueue.main.async {
self.newCollection.reloadData()
}
} catch {
print("Couldn't Fetch Data")
}
}

link to Core Data Pic

pic of collection view cell

最佳答案

var people: [People] = [] //Any suitable variable

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = newCollection.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! CustomeCell
cell.backgroundColor = .white
cell.layer.cornerRadius = 5
cell.layer.shadowOpacity = 3
cell.textLabel.text = people[indexPath.row].name //retrieve the name from your array
return cell
}



func fetchData() {
do {
people = fetchPeople()
DispatchQueue.main.async {
self.newCollection.reloadData()
}
} catch {
print("Couldn't Fetch Data")
}
}

func fetchPeople() -> [People] {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return [] }
let context = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "PeopleEntity")
let items = try! context.fetch(fetchRequest)
var detail: [People] = []
for data in items as! [NSManagedObject] {
var p = People(name: (data.value(forKey: "name") as? String) ?? "N/A") //check for nil
detail.append(p)
}
return detail
}

关于ios - 从核心数据获取的代码未在表格 View 单元格标签中正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56573495/

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