菜鸟教程小白 发表于 2022-12-11 20:00:38

ios - 如何使用 Core Data 按部分按字母顺序对 tableView 中的数据进行排序?


                                            <p><p>我是 Swift 3 编码的新手。
我正在尝试从 iPhone “复制”电话应用程序,但是在单元格中显示数据时遇到了一些问题,它们没有出现(当那里显然有一些数据时,从 Core Data 类中恢复)。
Core Data 类由一个带有一些属性的联系人组成,如“firstName”、“lastName”、“phoneNumber”等。我在 X.xcdatamodeld 中创建了它。那些属性
设置在另一个 VC 中并保存在那里。
我想在单元格中显示的是按字母顺序按部分排序的每个联系人的名字,例如电话应用程序。
这是我目前所拥有的。</p>

<pre><code>extension Contact {
var titleFirstLetter: String {
    return String(firstName!).uppercased()
}
}

class MainTableViewController: UITableViewController {
var listOfContacts = ()
var sortedFirstLetters: = []
var sections: [] = [[]]

struct Storyboard {
    static let cellIdentifier = &#34;Cell&#34;
    static let showDetailIdentifier = &#34;showDetail&#34;
    static let showInformationIdentifier = &#34;showInformationVC&#34;
}

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self

    let firstLetters = listOfContacts.map { $0.titleFirstLetter }

    let uniqueFirstLetters = Array(Set(firstLetters))

    sortedFirstLetters = uniqueFirstLetters.sorted()
    sections = sortedFirstLetters.map { firstLetter in
      return listOfContacts.filter { $0.titleFirstLetter == firstLetter }.sorted { $0.firstName! &lt; $1.firstName! }
    }
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    getData()
    tableView.reloadData()
}

func getData() {
    // 1. Create context
    let context = CoreDataController.persistentContainer.viewContext

    // 2. RecoverData from Database with fetchRequest
    do {
      try listOfContacts = context.fetch(Contact.fetchRequest())
    } catch {
      print(&#34;Error \(error.localizedDescription)&#34;)
    }
}

// MARK: - Tableview data source

override func numberOfSections(in tableView: UITableView) -&gt; Int {
   return sections.count
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
    return sections.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
    let contact = sections

    let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.cellIdentifier, for: indexPath)
    cell.textLabel?.text = contact.firstName

    return cell
}

override func sectionIndexTitles(for tableView: UITableView) -&gt; ? {
    return sortedFirstLetters
}


override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -&gt; String? {
   return sortedFirstLetters
}
</code></pre>

<p>注意:CoreDataController 是我在管理检索和保存到 CoreData 时使用的一个类(我所做的是从 AppDelegate.swift 复制生成的 CoreData 代码)
希望您能帮助我弄清楚为什么它不起作用。提前致谢!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><blockquote>
<p>Should use NSSortDescriptor with your fetched query like:</p>
</blockquote>

<pre><code>let sectionSortDescriptor = NSSortDescriptor(key: &#34;first_name&#34;, ascending: true)
let sortDescriptors =
fetchRequest.sortDescriptors = sortDescriptors
let fetchedPerson = try context.fetch(fetchRequest) as!
</code></pre>

<p>它可能会解决您的问题。如果您在此之后遇到问题,请告诉我。 </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何使用 Core Data 按部分按字母顺序对 tableView 中的数据进行排序?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/44860316/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/44860316/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何使用 Core Data 按部分按字母顺序对 tableView 中的数据进行排序?