2016-04-27 34 views
0

我想重新創建由蘋果公司製造的蟲蟲CNContactPickerViewController,所以我有一個數據數組[CNContact],我需要在UITableView中整齊地顯示數組。這一切都很好,直到我嘗試根據聯繫人的姓氏的第一個字母添加到該表格。我發現的唯一解決方案是遍歷[CNContact]數組,並根據其首字母將每個聯繫人手動分組爲一個字典,結果爲[String:[CNContact]]。有一個更好的方法嗎?UITableViewController部分

最終結果可以在下面的截圖中查看。 enter image description here

回答

0

這將按姓氏排序您的聯繫人。可能會矯枉過正,因爲你只希望他們按姓氏的第一個字母分組,而這將使用整個姓名進行排序。

var contacts :[CNContact] = [CNContact](); 
    // fill your contacts here 
    contacts.sortInPlace { (contact1, contact2) -> Bool in 
     contact1.familyName.compare(contact2.familyName) == .OrderedAscending 
    } 

不確定是否需要原始訂購。如果您想要原始數組,請創建舊數組的副本,並對副本進行排序。

相關問題