how to sort 2 attributes called from coredata in a tableview cell (swfit4)

天涯浪子 提交于 2019-12-13 03:08:54

问题


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

}

回答1:


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)]


来源:https://stackoverflow.com/questions/51193137/how-to-sort-2-attributes-called-from-coredata-in-a-tableview-cell-swfit4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!