【发布时间】:2017-06-30 13:02:02
【问题描述】:
我按字母顺序对 Realm 的数据进行了排序。
但我想在我的表格视图中显示它,就像内置 iPhone 应用程序中的联系人列表一样,具有从 A 到 Z 的不同部分。
我该怎么办?
主视图控制器:
class MainVC: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var contact_tableview: UITableView!
let realm = try! Realm()
var ContactList: Results<ContactObjectss> {
get {
// return realm.objects(ContactObjectss.self)
return realm.objects(ContactObjectss.self).sorted(byKeyPath: "first_name", ascending: true)
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.titleTextAttributes =
[NSFontAttributeName: UIFont(name: "IRANSansWeb-Medium", size: 17)!]
contact_tableview.delegate = self
contact_tableview.dataSource = self
}
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
}
}
ContactObjectss:
class ContactObjectss : 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 = ""
dynamic var picture : NSData?
}
【问题讨论】:
标签: swift uitableview sorting realm