sectionForSectionIndexTitle和sectionIndexTitlesForTableView未在swift 3中调用

shr*_*ani 1 iphone ios swift swift3

我在项目中使用了联系人Book,所有数据显示为sectionWise.Tableview部分索引不显示该实现sectionForSectionIndexTitle和sectionIndexTitlesForTableView但不调用.

为此我使用了以下代码:

let 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"]
func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int
{

    return ([UITableViewIndexSearch] + UILocalizedIndexedCollation.current().sectionIndexTitles as NSArray).index(of: title) - 1
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]!
{
    return arrIndexSection as [AnyObject]
}
Run Code Online (Sandbox Code Playgroud)

任何人都有上述解决方案的想法或解决方案,请帮助我.

谢谢.

Bal*_*ala 6

更改以下给出的部分方法

func numberOfSections(in tableView: UITableView) -> Int {
    return arrIndexSection.count
}

func sectionIndexTitles(for tableView: UITableView) -> [String]? {
    return arrIndexSection
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 44
}
Run Code Online (Sandbox Code Playgroud)

检查以下几点

  • 检查与您的tableview相关联的tableView IBOutlet.
  • 确保您的tableview数据源和委托与相应的类连接
  • 如果您需要多个部分,请确保在代码中为tableView数据源方法提供了部分编号.