问题
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