我在UITableView中加载联系人,我想在右端添加索引,供用户浏览联系人。
我正在使用代码:
var indexOfNames = [String]()
let indexNames = "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"
indexOfNames = indexNames.componentsSeparatedByString(" ")
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return indexOfNames
}
func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
let temp = indexOfNames as NSArray
return temp.indexOfObject(title)
}
但它不起作用。当我点击索引时,表格视图会显示在顶行。即A
。我已经为tableview设置了委托。也许Swift 2.0存在一些问题?
答案 0 :(得分:4)
为我工作的UITableView添加索引可能对您有帮助(对于swift 2.0):
var arrIndexSection : NSMutableArray = ["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"]
//MARK:- TableView Datasource/DataDelegate Method
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return arrIndexSection.count
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return arrIndexSection.objectAtIndex(section) as? String
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return self.arrIndexSection as? [String]
}