ios - 快速按字母顺序加载联系人列表

标签 ios swift contacts

我在 View Controller 中加载电话联系人时遇到小问题,我成功地在 Tableview 中加载了我的电话联系人姓名和号码,但联系人随机加载姓名,我希望联系人姓名必须是等于数字的字母顺序

这是我的示例代码:

func contactDetail () {

    var myPhValue:NSString!
    var myPBfirstname:NSString!

    let qualityOfServiceClass = QOS_CLASS_BACKGROUND
    let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
    dispatch_async(backgroundQueue,  {
        let addressBook : ABAddressBookRef? = ABAddressBookCreateWithOptions(nil, nil).takeRetainedValue()
        ABAddressBookRequestAccessWithCompletion(addressBook, { (granted : Bool, error: CFError!) -> Void in
            if granted == true {
                let allContacts : NSArray = ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue()
                for contactRef:ABRecordRef in allContacts { // first name
                    myPBfirstname = ABRecordCopyValue(contactRef, kABPersonFirstNameProperty)?.takeRetainedValue() as! NSString? ?? ""
                    let myPBlastname = ABRecordCopyValue(contactRef, kABPersonLastNameProperty)?.takeRetainedValue() as! NSString? ?? ""
                    let phonesRef: ABMultiValueRef = ABRecordCopyValue(contactRef, kABPersonPhoneProperty)?.takeRetainedValue() as ABMultiValueRef? ?? ""
                    var phonesArray  = Array<Dictionary<String,String>>()
                    for var i:Int = 0; i < ABMultiValueGetCount(phonesRef); i++ {
                        let myPhLabel = ABMultiValueCopyLabelAtIndex(phonesRef, i)?.takeRetainedValue() as NSString? ?? ""
                        myPhValue = ABMultiValueCopyValueAtIndex(phonesRef, i)?.takeRetainedValue() as! NSString? ?? ""
                        if myPhLabel.containsString("Mobile") {

                        }
                    }
                    let nameString = " "+(myPBlastname as String)
                    let nameValues = (myPBfirstname as String)+(nameString as String)
                    let contactDict : Dictionary = ["name": nameValues,"phone":myPhValue]
                    self.contactArray.addObject(contactDict)

                    if self.contactArray.count == 0 {
                        NSLog("contactValue is Zero")
                    }
                    else{
                        NSLog("count Value %d", self.contactArray.count)
                    }

                }


            }

            dispatch_async(dispatch_get_main_queue(), { () -> Void in

                self.contactTable.hidden = false
                self.contactTable.reloadData()


            })
        })
    })

}



func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
 return contactArray.count
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
     let cell:UITableViewCell = self.contactTable.dequeueReusableCellWithIdentifier("cell") as UITableViewCell!
    cell.selectionStyle = UITableViewCellSelectionStyle.None

    cell.preservesSuperviewLayoutMargins = false
    cell.separatorInset = UIEdgeInsetsZero
    cell.layoutMargins = UIEdgeInsetsZero



    cell.textLabel!.text = contactArray.valueForKey("name").objectAtIndex(indexPath.row) as? String

    return cell

}

最佳答案

正如我从您的代码中假设的那样,您的 contactArray 设置了如下值:

let contactArray = [["name": "tim", "phone": 123], ["name": "adi", "phone": 1234], ["name": "cora", "phone": 456], ["name": "berta", "phone": 678]]

要按名称排序,您可以使用 sort method :

let sortedContactArray = contactArray.sort{ ($0["name"] as! String) < ($1["name"] as! String) }

// result: [["phone": 1234, "name": adi], ["phone": 678, "name": berta], ["phone": 456, "name": cora], ["phone": 123, "name": tim]]

关于ios - 快速按字母顺序加载联系人列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36302719/

相关文章:

ios - 如何使分段控件显示不同数量的单元格?

ios - Swift:XCode6 Beta 5 在 AppDelegate 中给出核心数据对象的错误

android - 无法使用 ContentProviderOperation 删除整个联系人

ios - 无法在核心数据中保存数据

ios - 如何在 iPad 的弹出 View 中隐藏导航栏?

ios - 在 Swift 中点击 UISwitch 时选择 UITableView 的行

android - 检索所有附加了特定帐户的联系人

java - 如何访问联系人

objective-c - 为图像添加色调

iphone - 如何从服务器解析 XML 文件?