Swift 如何按字母顺序对tableView单元格中的一段数据(CoreData)进行排序?
我正在尝试制作一个简单的“联系人”应用程序。一切正常,但我找不到一种方法将CoreData中的数据按字母顺序写入tableView 我希望人们在CoreData中保存他们的电话号码和姓名,然后,他们将作为Person对象添加到数组中。另外,我想将名称写入tableView单元格标签。正如你所理解的,这些名字必须按字母顺序排列 我用MVC创建了这个应用程序,并创建了一个空数组来保存用户的电话号码和姓名。数组的类型为PersonSwift 如何按字母顺序对tableView单元格中的一段数据(CoreData)进行排序?,swift,xcode,tableview,Swift,Xcode,Tableview,我正在尝试制作一个简单的“联系人”应用程序。一切正常,但我找不到一种方法将CoreData中的数据按字母顺序写入tableView 我希望人们在CoreData中保存他们的电话号码和姓名,然后,他们将作为Person对象添加到数组中。另外,我想将名称写入tableView单元格标签。正如你所理解的,这些名字必须按字母顺序排列 我用MVC创建了这个应用程序,并创建了一个空数组来保存用户的电话号码和姓名。数组的类型为Person //Like below. // I hold the informa
//Like below.
// I hold the informations in this array.
var personList = [Person]()
//here is the cell format I am using in the app.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as! TableViewCell
let person = personList[indexPath.row]
cell.imageBackground.tintColor = cell.backgroundColors.randomElement()
cell.imageLabel.text = String(person.personFirstLetter.first!)
cell.nameLabel.text = person.personName
return cell
}
//this is how I get data from the user.
@objc func getData() {
personList.removeAll(keepingCapacity: false)
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "People")
fetchRequest.returnsObjectsAsFaults = false
do {
let results = try context.fetch(fetchRequest)
for result in results as! [NSManagedObject]{
if let name = result.value(forKey: "name") as? String{
if let phoneNumber = result.value(forKey: "number") as? String{
if let firstLetter = result.value(forKey: "letter") as? String{
personList.append(Person(personName: name, personNumber: phoneNumber, personFirstLetter: firstLetter))
}
}
}
}
} catch {
print(error.localizedDescription)
}
//如下所示。
//我保存了这个数组中的信息。
变量personList=[Person]()
//这是我在应用程序中使用的单元格格式。
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
让cell=tableView.dequeueReusableCell(标识符为:“tableViewCell”,for:indexath)作为!tableViewCell
让person=personList[indexPath.row]
cell.imageBackground.tintColor=cell.backgroundColors.randomElement()
cell.imageLabel.text=字符串(person.personFirstLetter.first!)
cell.namelab.text=person.personName
返回单元
}
//这就是我从用户那里获取数据的方式。
@objc func getData(){
personList.removeAll(保留容量:false)
让上下文=(UIApplication.shared.delegate为!AppDelegate)。persistentContainer.viewContext
let fetchRequest=NSFetchRequest(entityName:“人”)
fetchRequest.returnsObjectsAsFaults=false
做{
let results=try context.fetch(fetchRequest)
对于结果,结果为![NSManagedObject]{
如果让name=result.value(forKey:“name”)作为?字符串{
如果让phoneNumber=result.value(forKey:“number”)作为?字符串{
如果让firstLetter=result.value(forKey:“letter”)作为?字符串{
append(Person(personName:name,personNumber:phoneNumber,personFirstLetter:firstLetter))
}
}
}
}
}抓住{
打印(错误。本地化描述)
}
我真的不知道在这里分享什么,如何或在哪里采用字母排序
谢谢大家。干杯!最有效的方法是在获取记录时对记录进行排序。并在请求中使用真实类型,而不是未指定的
NSFetchRequestResult
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let fetchRequest = NSFetchRequest<People>(entityName: "People")
fetchRequest.sortDescriptors = [NSSortDecriptor(key: "name", ascending: true)]
这是获取数据的全部代码,只有一行
do {
personList = try context.fetch(fetchRequest)
} catch {
print(error.localizedDescription)
}
cellForRow
中的变化相当微妙
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as! TableViewCell
let person = personList[indexPath.row]
cell.imageBackground.tintColor = cell.backgroundColors.randomElement()
cell.imageLabel.text = String(person.letter.first!)
cell.nameLabel.text = person.name
return cell
}
这对我帮助很大!谢谢你的详细解释!:)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as! TableViewCell
let person = personList[indexPath.row]
cell.imageBackground.tintColor = cell.backgroundColors.randomElement()
cell.imageLabel.text = String(person.letter.first!)
cell.nameLabel.text = person.name
return cell
}