【问题标题】:Adding index at uitableview is not working in Swift 2.0在 uitableview 添加索引在 Swift 2.0 中不起作用
【发布时间】:2015-09-26 13:02:30
【问题描述】:

我正在 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 有一些问题?

【问题讨论】:

  • temp.indexOfObject(title) 返回什么值?您的表格是否有 26 个部分,所有部分都有行?
  • temp.indexOfObject(title) 是所有行数据的索引。即 A、B、C ......并且不,我没有 26 个部分。但我不知道如何编码
  • 我的问题实际上是关于您作为索引返回的数字是否实际上与表格可以合理转到的某个部分匹配。
  • 不,我不这么认为。

标签: ios swift uitableview swift2


【解决方案1】:

为我工作的 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]
    }

【讨论】:

    猜你喜欢
    • 2021-04-22
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多