我在表格视图中实现了sectionIndex,只有向下滚动并加载所有数据后,总字母才能正常工作。该功能仅对首页有效。
arrIndexSection = ["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"]
enter code here
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return arrIndexSection
}
如果您知道什么,请帮助我
//MARK:- Tableview methods
func numberOfSections(in tableView: UITableView) -> Int {
return sectionTitles.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return contactsWithSections[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MemberDirectoryTableViewCell") as! MemberDirectoryTableViewCell
let contact = contactsWithSections[indexPath.section][indexPath.row]
return cell
}
// arrIndexSection = func sectionIndexTitles(for tableView:UITableView)-> [String]? {
return arrIndexSection
}
extension UILocalizedIndexedCollation {
//func for partition array in sections
//func for partition array in sections
func partitionObjects(array:[AnyObject], collationStringSelector:Selector) -> ([AnyObject], [String]) {
var unsortedSections = [[AnyObject]]()
//1. Create a array to hold the data for each section
for _ in self.sectionTitles {
unsortedSections.append([]) //appending an empty array
}
//2. Put each objects into a section
for item in array {
let index:Int = self.section(for: item, collationStringSelector:collationStringSelector)
unsortedSections[index].append(item)
}
//3. sorting the array of each sections
var sectionTitles = [String]()
var sections = [AnyObject]()
for index in 0 ..< unsortedSections.count { if unsortedSections[index].count > 0 {
sectionTitles.append(self.sectionTitles[index])
sections.append(self.sortedArray(from: unsortedSections[index], collationStringSelector: collationStringSelector) as AnyObject)
}
}
return (sections, sectionTitles)
}
}