本文介绍了如何对一个tableview单元中的coredata调用的2个属性进行排序(swfit4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码在单个tableview单元中打印2个核心数据属性。我希望每个单元格都以这种方式进行排序。 attr 1 a-z attr 2 z -a。意味着如果集合是(b,b),(b,a),(a,a)。它将打印(a,a),(b,a),(bb)。

The code below prints 2 core data attributes in a single tableview cell. I would like each cell to be sorted in a this way. attr 1 a - z attr 2 z -a. Meaning that if the sets are (b,b),(b,a),(a,a). It would print (a,a),(b,a),(bb).

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let title = itemsName[indexPath.row]

    let cell = tazbleView.dequeueReusableCell(withIdentifier: "cell", for : indexPath)

    let attr1 = title.value(forKey: "lorde") as? String
    let attr2 = title.value(forKey: "num") as? String
    cell.textLabel?.text = [attr1, attr2].flatMap { $0 }.reduce("", +)

    return cell

}


推荐答案

在您的提取请求中添加排序描述符以获取对象正确的顺序

Add sort descriptors to your fetch request to get the objects in the right order

let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Team")
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "lorde", ascending: true),NSSortDescriptor(key: "num", ascending: false)]

这篇关于如何对一个tableview单元中的coredata调用的2个属性进行排序(swfit4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 13:31