在tableView上创建没有节的indexList吗?

时间:2018-12-17 04:40:43

标签: swift indexing tableview

我的表视图有一个部分,其中一个数组显示为行。我正在尝试创建一个索引列表,该索引列表显示在我可以自定义的右侧,并将跳转到特定行。到目前为止,我已经能够显示索引,但是单击一个字母时它不会跳到特定的行。当按下“ A”时,它将跳回到第一行,但其他索引选择均无效。有解决方法吗?

编辑:一直在研究答案,但仍然找不到解决该问题的方法。

   let respiratory = ["A - Patient Disposition Form","B - On Scene Physician","C - APGAR Score","D - LA Prehospital Stroke Screen","E - Pain Scales","F - Restraint Checklist","G - Abbreviations","H - Reperfusion Checklist","I - Difficult Airway","J - Burn Resources","DNR","NCCEP Airway Evaluation Form","NC MOST Form","PREMIS Preliminary Report","NC Eye Bank"]

        let indexList = ["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","AA"]

        func sectionIndexTitles(for tableView: UITableView) -> [String]? {
            return indexList;
        }
        func sectionIndexTitlesForTableView( for tableView: UITableView) -> [AnyObject]! {
            return respiratory as [AnyObject]
        }
 @IBOutlet weak var animalTableView: UITableView!




    ///Set elements in cell
    func tableView(  _ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "appendicieslist"/*Identifier*/, for: indexPath)
        cell.textLabel?.text = respiratory[indexPath.row]
        // FONT STYLE
        cell.textLabel?.textColor = UIColor.white
        cell.detailTextLabel?.font = UIFont.boldSystemFont(ofSize: 30.0)
        cell.textLabel?.textAlignment = .center
        self.animalTableView.backgroundColor = UIColor.clear
        cell.textLabel?.backgroundColor = UIColor.black

        return cell
    }



    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return respiratory.count
    }

2 个答案:

答案 0 :(得分:0)

所要抓住的是,您正在使用一个表视图,其中包含一个包含许多行的部分(而不是包含许多具有一行的部分),但是索引通过引用部分而不是行来进行操作。

摘自UITableView类文档:

  

表视图的索引可以在表的右侧以条形显示(例如,“ A”至“ Z”)。您可以触摸特定标签以跳转到目标部分

一旦移至基于节的表视图,就可以通过实现可选功能tableView(_:sectionForSectionIndexTitle:at:)对索引提供的导航进行更细粒度的控制。

始终可以编写自己的自定义索引,该自定义索引复制行而不是节的UITableView索引的功能,但是根据您的使用情况,效率可能很低或不合适。

答案 1 :(得分:0)

这很hacky,但是我认为您可以在columnspacing的顶部添加一个defer语句来处理过渡。不知道在大列表中看起来是否不错,但是值得尝试。

sectionForSectionIndexTitle