【问题标题】:code being fetch from core data is not being displayed correctly in tableview cell label从核心数据获取的代码未在 tableview 单元格标签中正确显示
【发布时间】: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")
    }
}

link to Core Data Pic

pic of collection view cell

【问题讨论】:

  • 每次单元格重新加载时,您都在检索数据。请在viewDidLoad 期间将其删除并预取数据,以便您仅将其与cellForItemAt 中的视图绑定。
  • 我在我的问题和新图片链接中添加了更多方法,你能看吗?同样的问题。
  • 您正在添加对象 Jessica 的 String。请将其转换为您的对象类并从该对象中检索名称属性以在标签中分配。结果不是String。从cellForItemAt中删除获取部分

标签: ios core-data uicollectionviewcell collectionview swift5


【解决方案1】:
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
}

【讨论】:

  • 我试过你的代码。我可以输入代码,但是当我尝试使用集合视图加载视图控制器时,我收到此错误致命错误:索引超出范围。我在上面添加了我的问题。非常感谢。
  • @MaxMadill 你把要从coredata获取的代码放在哪里了;在 collectionView cellForItem 代表里面的那个?这应该在 fetchData() 内部,在获取数据后将名称数组分配给people。请更新代码以便我提供帮助:)
  • 我对如何获取变量感到困惑。你能告诉我如何获取数据吗?例如,我试图从名为沙拉的实体中获取属性字符串 bat。谢谢。
  • @MaxMadill:我已经更新了代码。我希望这能帮助您获取数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-25
  • 1970-01-01
  • 2020-08-01
  • 2021-06-26
  • 1970-01-01
相关资源
最近更新 更多