2016-08-21 27 views
1

// MARK在TableView中顯示聯繫人:的UITableViewDelegate的iOS斯威夫特 - 使用UILocalizedIndexedCollat​​ion

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String { 
    print("titleForHeaderInSection: \(collation.sectionTitles[section])") 
    return collation.sectionTitles[section] 
} 

override func sectionIndexTitlesForTableView(tableView: UITableView) -> [String] { 
    print("sectionIndexTitlesForTableView: \(collation.sectionIndexTitles)") 
    return collation.sectionIndexTitles 
} 

override func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int { 
    print("sectionForSectionIndexTitle: \(sections.count)") 
    return collation.sectionForSectionIndexTitleAtIndex(index) 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return (self.results.count > 0) ? self.results.count : 0 
} 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    let numberOfSections = UILocalizedIndexedCollation.currentCollation().sectionTitles.count 
    print("numberOfSections: \(numberOfSections)") 
    return numberOfSections 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("contactscell") as UITableViewCell! 
    let label    = cell.viewWithTag(1) as? UILabel 
    label?.text   = self.results[indexPath.row].givenName 
    return cell 
} 

它顯示在每節的所有聯繫人。我想按排序順序顯示聯繫人,並使用字母索引a到z

回答

0

無論您處於哪個部分,您都要返回numberOfRowsInSection中的self.results.count。必須返回每個字母的行數。考慮使用字母作爲鍵和聯繫人作爲值的字典。