【问题标题】:sort Realm data in a tableView alphabetically by section Swift 3按 Swift 3 部分的字母顺序对 tableView 中的 Realm 数据进行排序
【发布时间】: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


    【解决方案1】:

    我认为你可以通过制作 26 个部分(每个字母一个字母)来做到这一点,然后计算每个部分的 ContactObjects 数量。

    func numberOfSections(in tableView: UITableView) -> Int {
            return 26 // letters of the alphabet
    }
    

    我假设您的 ContactObjects 是根据他们的名字按字母顺序排序的,如果它们不只是将 first_name 替换为 last_name

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            let sectionLetter = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z".components(separatedBy: ",")[section-1]
            let contactsWithLetter = ContactList.filter {String($0.first_name.lowercased()[$0.first_name.startIndex]) == sectionLetter} //had to use the startIndex thing because apparently $0.first_name is an inout property and therefore you can't just get it's characters
            return contactsWithLetter.count
        }
    

    【讨论】:

    • 发生错误兄弟:致命错误:索引超出范围
    • 您可以尝试将[section-1] 替换为[section] 吗?也许这些部分从 0 而不是 1 开始。
    猜你喜欢
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多