【问题标题】:how to sort 2 attributes called from coredata in a tableview cell (swfit4)如何对从 tableview 单元格中的 coredata 调用的 2 个属性进行排序(swfit4)
【发布时间】:2018-12-14 01:03:27
【问题描述】:

下面的代码在单个表格视图单元格中打印 2 个核心数据属性。我希望每个单元格都以这种方式排序。属性 1 a - z 属性 2 z -a。这意味着如果集合是(b,b),(b,a),(a,a)。它将打印 (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

}

【问题讨论】:

    标签: uitableview sorting core-data tableview swift4


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-02
      相关资源
      最近更新 更多