带部分的 tableview 搜索栏
Posted
技术标签:
【中文标题】带部分的 tableview 搜索栏【英文标题】:tableview search bar with section 【发布时间】:2018-05-10 18:05:45 【问题描述】:在问你之前,我遵循了 3 个教程,但没有任何结果。 我有一个包含 20 个部分的表格视图,每个单元格都有 2 个标签:1 个用于标题,1 个用于艺术家。所以我有 20 个按字母顺序排列的标题数组和 20 个按字母顺序排列的艺术家数组。 我想在这两个数组之间实现一个搜索栏,仅基于标题数组。 有可能吗?
这是我的代码
import UIKit
class TableViewController: UITableViewController
//Creo le sezioni
let sections = ["A","B","C","D","E","F","G","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","Z"]
let cantiTitles: [String] = ["A","B","C","D","E","F","G","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","Z"]
var categoriacliccata = 0
var rowCliccata = 0
@IBOutlet weak var searchBar: UISearchBar!
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad()
super.viewDidLoad()
override func sectionIndexTitles(for tableView: UITableView) -> [String]?
return cantiTitles
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
print("section: \(indexPath.section)")
categoriacliccata = indexPath.section
print("row: \(indexPath.row)")
rowCliccata = indexPath.row
performSegue(withIdentifier: "segue_testo", sender: self)
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
if (segue.identifier == "segue_testo")
let secondVC: TextView_Controller = segue.destination as! TextView_Controller
secondVC.recivedCategoria = categoriacliccata
secondVC.recivedRow = rowCliccata
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
return sections[section]
override func numberOfSections(in tableView: UITableView) -> Int
return sections.count
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
switch section
case 0:
return titoliA.count
case 1:
return titoliB.count
case 2:
return titoliC.count
case 3:
return titoliD.count
case 4:
return titoliE.count
case 5:
return titoliF.count
default:
return 0
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "cellaCustom") as! Cella
switch indexPath.section
case 0:
cell.lbl_titolo.text = titoliA[indexPath.row]
cell.lbl_artista.text = artistaA[indexPath.row]
break
case 1:
cell.lbl_titolo.text = titoliB[indexPath.row]
cell.lbl_artista.text = artistaB[indexPath.row]
break
case 2:
cell.lbl_titolo.text = titoliC[indexPath.row]
cell.lbl_artista.text = artistaC[indexPath.row]
break
case 3:
cell.lbl_titolo.text = titoliD[indexPath.row]
cell.lbl_artista.text = artistaD[indexPath.row]
break
case 4:
cell.lbl_titolo.text = titoliE[indexPath.row]
cell.lbl_artista.text = artistaE[indexPath.row]
break
case 5:
cell.lbl_titolo.text = titoliF[indexPath.row]
cell.lbl_artista.text = artistaF[indexPath.row]
break
default:
break
return cell
【问题讨论】:
是的,这是可能的。但显然,只使用一个大数组,不要将它们分开。您的数据不应该去同步化。对于搜索栏,请使用filter()
。现在搜索您的数据已同步更有意义,因此请仅使用一个大数组。
【参考方案1】:
首先,您需要将此代码添加到您的 viewDidLoad 方法中:
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Events"
navigationItem.searchController = searchController
definesPresentationContext = true
接下来,在你的类中添加这个委托方法:
func filterContentForSearchText(_ searchText: String, scope: String = "All")
//Filter artists based on searchText here
self.reloadData()
【讨论】:
如果您有任何问题,请尽管问 你能给我一个教程,我可以在其中找到如何实现 filterContentForSearchText 吗?? 这个答案应该可以帮助***.com/questions/41663233/tableview-search-in-swift以上是关于带部分的 tableview 搜索栏的主要内容,如果未能解决你的问题,请参考以下文章