ios - sectionForSectionIndexTitle 和 sectionIndexTitlesForTableView 未在 swift 3 中调用

标签 ios iphone swift swift3

我在项目中使用了联系簿,所有数据显示为 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]
}

任何人对上述解决方案有想法或解决方案,请帮助我。

谢谢。

最佳答案

更改部分方法如下所示

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
}

检查以下几点

  • 检查与您的 tableview 连接的 tableView IBOutlet。
  • 确保您的 tableview 数据源和委托(delegate)已连接到相应的类
  • 如果您想要多个部分,请确保您在代码中为 tableView 数据源方法提供了部分的数量。

关于ios - sectionForSectionIndexTitle 和 sectionIndexTitlesForTableView 未在 swift 3 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44457427/

相关文章:

iphone - 如何让 CoreData Debug 参数输出到控制台?

SwiftySKScrollView 工作完美,直到我尝试更改其大小或移动它

ios - 错误 : Use of unresolved identifier 'indexPath'

ios - Xcode - 在我的应用程序上启用 iTunes 文件共享,但它没有出现在 iTunes 的列表中?

ios - UITableView 在旋转时自动布局调整大小

ios - 应用程序更新时崩溃

ios - 如何在 Swift 中创建自定义段控件

iphone - 在 iPhone 中查找录音的长度

ios - UIScrollview 下的 UIImageView 不适合顶部屏幕

ios - 如何将ios应用程序转换为Xamarin.ios应用程序?