首页 > 解决方案 > sectionIndexTitles 不工作

问题描述

我在我的项目中添加了下一个代码:

    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"]

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

但是在单击任何字母后,tableview 只会滚动到我的部分的 B 字母。

我的 TableViewController 的完整代码:

class TagsController: UITableViewController {
    
    var hashtags = [HashTags]()
    var filtered = [HashTags]()
        
    let searchController = UISearchController(searchResultsController: nil)

    var searchBarIsEmpty: Bool {
        guard let text = searchController.searchBar.text else { return false }
        return text.isEmpty
    }
    
    var isFiltering: Bool {
        return searchController.isActive && !searchBarIsEmpty
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 135
                
        searchController.searchResultsUpdater = self
        searchController.obscuresBackgroundDuringPresentation = false
        searchController.searchBar.placeholder = "Search"
        navigationItem.searchController = searchController
        definesPresentationContext = true
    }
    


    // MARK: - Table view data source
    
    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"]

    override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
       return arrIndexSection
   }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        if isFiltering {
            return filtered.count
        }

        return hashtags.count
    }
    
    
    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if isFiltering {
            return filtered[section].title
        }

        return hashtags[section].title
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Tags", for: indexPath)
        
        var item = HashTags()

        if isFiltering {
            item = filtered[indexPath.section]
        } else {
            item = hashtags[indexPath.section]
        }


        let tags = item.tags
    
        cell.textLabel?.text = tags.joined(separator: ", ")

        return cell
    }

}

extension TagsController: UISearchResultsUpdating {
    
    func updateSearchResults(for searchController: UISearchController) {
        filterContentForSearchText(searchController.searchBar.text!)
    }
    
    func filterContentForSearchText(_ searchText: String) {
        filtered = hashtags.filter({ (hashtags: HashTags) in return hashtags.tags.contains(where: { (aTag: String) in aTag.contains(searchText.lowercased()) }) })
        tableView.reloadData()
    }
    
}

问题可能出在哪里?

标签: iosswifttableview

解决方案


除非您始终按字母顺序将 26 个部分完全分组,否则您的代码毫无意义。那可能性有多大?你有一段以 X 开头的单词?即使在过滤时??我对此表示怀疑。

一般不要硬编码arrIndexSection。相反,sectionIndexTitles根据当时实际存在的部分动态返回结果,就像您为numberOfSections. 每个部分一个sectionIndexTitles数组元素。


推荐阅读