【发布时间】:2019-10-27 15:13:52
【问题描述】:
我正在从核心数据中获取并将其打印在标签上。标签上打印的问题有很多流失的东西。正如您在下面的照片中看到的那样。在每个集合视图单元格中,我希望它打印数组的 1 个元素。所以如果数组有 [vanessa,taylor,bastista]。集合视图单元格应打印 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")
}
}
【问题讨论】:
-
每次单元格重新加载时,您都在检索数据。请在
viewDidLoad期间将其删除并预取数据,以便您仅将其与cellForItemAt中的视图绑定。 -
我在我的问题和新图片链接中添加了更多方法,你能看吗?同样的问题。
-
您正在添加对象 Jessica 的
String。请将其转换为您的对象类并从该对象中检索名称属性以在标签中分配。结果不是String。从cellForItemAt中删除获取部分
标签: ios core-data uicollectionviewcell collectionview swift5