gpt4 book ai didi

uitableview - 向 searchBar 添加索引

转载 作者:行者123 更新时间:2023-12-02 03:20:34 24 4
gpt4 key购买 nike

需要帮助来更正此代码。
我有一个带有餐厅名称和地址的核心数据应用程序,有一个搜索栏,它正在运行。我要添加的是一个 Index 和一个 IndexTitle,如下图(箭头)所示。
非常欢迎任何帮助。提前致谢。

import UIKit
import CoreData

class DictionaryTableViewController: UITableViewController, NSFetchedResultsControllerDelegate, UISearchResultsUpdating
{
var searchController:UISearchController!
var searchResults:[Dictionary] = []

private var dictionaryItems:[Dictionary] = []
var fetchResultController:NSFetchedResultsController!

override func viewDidLoad() {
super.viewDidLoad()

if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {

let fetchRequest = NSFetchRequest(entityName: "DictionaryEntity")
do {
dictionaryItems = try managedObjectContext.executeFetchRequest(fetchRequest) as! [Dictionary]
} catch {
print("Failed to retrieve record")
print(error)
}
}

searchController = UISearchController(searchResultsController: nil)
tableView.tableHeaderView = searchController.searchBar
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false

tableView.estimatedRowHeight = 30.0
tableView.rowHeight = UITableViewAutomaticDimension
}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 26
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

if searchController.active {
return searchResults.count
} else {
return dictionaryItems.count
}
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! DictionaryTableViewCell

// Configure the cell...
cell.wordLabel.text = dictionaryItems[indexPath.row].word
cell.definitionSmallLabel.text = dictionaryItems[indexPath.row].definition

let dictionary = (searchController.active) ? searchResults[indexPath.row]: dictionaryItems[indexPath.row]

// Configure the cell...
cell.wordLabel.text = dictionary.word
cell.definitionSmallLabel.text = dictionary.definition
return cell
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
return "dictionaryItems\(section)"
}

override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {

if searchController.active{
return false
}else{
return true
}
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDictionaryDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let destinationController = segue.destinationViewController as! DictionaryDetailViewController
destinationController.dictionary = (searchController.active) ? searchResults[indexPath.row] : dictionaryItems[indexPath.row]
searchController.active = false
}
}
}

func updateSearchResultsForSearchController(searchController:
UISearchController) {
if let searchText = searchController.searchBar.text {
filterContentForSearchText(searchText)
tableView.reloadData()
}
}

func filterContentForSearchText(searchText: String) {
searchResults = dictionaryItems.filter({ (dictionary:Dictionary) -> Bool in
let wordMatch = dictionary.word!.rangeOfString(searchText, options:
NSStringCompareOptions.CaseInsensitiveSearch)
return wordMatch != nil
})
}
}



我想在我的 TableView 中拥有的是索引(左侧的箭头)和索引标题(右侧的箭头)。

enter image description here

最佳答案

您在 titleForHeaderInSection 的覆盖函数中有错别字

这是更正后的,注意 tableView 中的 v 是大写的:(复制粘贴我的代码)

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

}

我建议使用 Xcode 的自动完成功能。这样,您就不会陷入这样的隐患。

更新:您还需要提供这两种方法才能查看章节标题和章节索引标题。

override func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return ["A", "B", "C"]
}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "A title for section: \(section)"
}

试试下面的代码:

class TableViewController: UITableViewController {

override func viewDidLoad() {
super.viewDidLoad()

tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 9
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 9
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

// Configure the cell...
cell.textLabel?.text = "\(indexPath.section+1)\(indexPath.row+1)"

return cell
}

override func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return ["A", "C", "B"]
}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "SECTION \(section+1)"
}

关于uitableview - 向 searchBar 添加索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33782129/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com