我正在从核心数据中获取数据并将其打印在标签上。标签上打印的问题有很多问题。如您在下面的照片中看到的。在每个集合视图单元格中,我希望它打印数组的1个元素。因此,如果数组具有[vanessa,taylor,bastista]。集合视图单元格应显示Vanessa。
target_include_directories(exe PRIVATE "../include")
更多方法
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
}
答案 0 :(得分:0)
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
}