gpt4 book ai didi

ios - 在 Swift 3 中按字母顺序对 Realm 对象排序

转载 作者:搜寻专家 更新时间:2023-10-31 21:51:17 25 4
gpt4 key购买 nike

我有一个电话簿想要按字母顺序对 Realm 对象(联系人)进行排序,我遵循了一些教程,但我有一些问题显示在图片中:

enter image description here

我的代码是:

主VC.swift :

import UIKit
import RealmSwift

class MainVC: UIViewController,UITableViewDelegate,UITableViewDataSource {

@IBOutlet weak var contact_tableview: UITableView!

var contactsWithSections = [[ContactItem]]()

var sectionTitles = [String]()

let realm = try! Realm()

var ContactList: Results<ContactItem> {
get {
return realm.objects(ContactItem.self)
}
}

override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.titleTextAttributes =
[NSFontAttributeName: UIFont(name: "IRANSansWeb-Medium", size: 17)!]
contact_tableview.delegate = self
contact_tableview.dataSource = self

let (arrayContacts, arrayTitles) = collation.partitionObjects(array: ContactList, collationStringSelector: #selector(getter: ContactItem.first_name))

}





func numberOfSections(in tableView: UITableView) -> Int {

return 1

}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {



return ContactList.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


let cell = tableView.dequeueReusableCell(withIdentifier: "ContactCell") as! ContactCell

let item = ContactList[indexPath.row]

cell.lblName!.text = item.first_name

return cell

}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

let ContactV = storyboard?.instantiateViewController(withIdentifier: "ViewContact") as! ContactInfoVC
navigationController?.pushViewController(ContactV, animated: true)
ContactV.ContactID = ContactList[indexPath.row]
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
contact_tableview.reloadData()
// to reload selected cell
}





}



let collation = UILocalizedIndexedCollation.current()

extension UILocalizedIndexedCollation {
//func for partition array in sections
func partitionObjects(array:[AnyObject], collationStringSelector:Selector) -> ([AnyObject], [String]) {
var unsortedSections = [[AnyObject]]()
//1. Create a array to hold the data for each section
for _ in self.sectionTitles {
unsortedSections.append([]) //appending an empty array
}
//2. Put each objects into a section
for item in array {
let index:Int = self.section(for: item, collationStringSelector:collationStringSelector)
unsortedSections[index].append(item)
}
//3. sorting the array of each sections
var sectionTitles = [String]()
var sections = [AnyObject]()
for index in 0 ..< unsortedSections.count { if unsortedSections[index].count > 0 {
sectionTitles.append(self.sectionTitles[index])
sections.append(self.sortedArray(from: unsortedSections[index], collationStringSelector: collationStringSelector) as AnyObject)
}
}
return (sections, sectionTitles)
}
}

联系人.swift :

    import UIKit
import RealmSwift



class ContactItem : Object {

dynamic var id:Int = 0
dynamic var first_name = ""
dynamic var last_name = ""
dynamic var work_email = ""
dynamic var personal_email = ""
dynamic var contact_picture = ""
dynamic var mobile_number = ""
dynamic var home_number = ""
dynamic var isFavorite = false
dynamic var Notes = ""

}

以及我如何对添加到第一个的最后一个联系人进行排序?就像 iPhone 中电话应用程序中的联系人选项卡 ...

问候<3

最佳答案

您需要使用 asc 对 Realm 的查询结果进行排序。例如试试这个:

var ContactList: Results<ContactItem> {
get {
return realm.objects(ContactItem.self).sorted(byKeyPath: "your property", ascending: true)
}
}

关于ios - 在 Swift 3 中按字母顺序对 Realm 对象排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44788115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com