tableview搜索栏与部分

时间:2018-05-10 18:05:46

标签: arrays swift tableview searchbar

在问你之前我遵循了3个教程但没有任何结果。 我有一个20个部分的tableview,每个单元格有2个标签:1为标题,1为艺术家。所以我按字母顺序排列20个数组,按字母顺序排列20个艺术家数组。 我想在这个2数组之间实现一个搜索栏,仅基于标题数组。 有可能吗?

这是我的代码

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
    }

}

enter image description here

1 个答案:

答案 0 :(得分:0)

首先,您需要将此代码添加到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()
}