【问题标题】:How to sort a piece of data (CoreData) in the tableView cell alphabetically?如何按字母顺序对tableView单元格中的一条数据(CoreData)进行排序?
【发布时间】:2021-05-18 19:05:01
【问题描述】:

我正在尝试制作一个简单的“联系人”应用程序。一切正常,但我找不到按字母顺序将数据从 CoreData 写入 tableView 的方法。

我希望人们将他们的电话号码和姓名保存在 CoreData 中,然后,他们将作为 Person 对象添加到数组中。另外,我想将名称写入 tableView 单元格标签。如您所知,这些名称必须按字母顺序排列。

我使用 MVC 创建了应用程序并创建了一个空数组来保存用户的电话号码和姓名。数组的类型是Person。

//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)
    }

我实际上不知道在这里分享什么以及如何或在哪里采用字母排序。

谢谢大家。干杯!

【问题讨论】:

    标签: swift xcode tableview


    【解决方案1】:

    最有效的方法是在获取记录时对其进行排序。并在请求中使用真实类型而不是未指定的NSFetchRequestResult

    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    let fetchRequest = NSFetchRequest<People>(entityName: "People")
    fetchRequest.sortDescriptors = [NSSortDecriptor(key: "name", ascending: true)]
    

    进一步不要将对象映射到其他类型,使用People类,声明数据源数组

    var personList = [People]()
    

    这是获取数据的全部代码,只有一行

    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
    }
    

    【讨论】:

    • 对我帮助很大!感谢您的详细解释! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多