Swift uitable视图单元格标记,搜索不起作用



我的场景,我正在尝试使用custom cellsections创建UITableview。在这里,我尝试在一次one cell进行checkmark选择。它无法正常工作。另外,在这种情况下搜索主要综合体之一。提供一些解决此问题的想法。

下面的代码我正在使用,

import UIKit
class CustomCell: UITableViewCell {
    @IBOutlet weak var myCellLabel: UILabel!
    override func setSelected(_ selected: Bool, animated: Bool) { // Check Mark Selection Not Properly Working
        super.setSelected(selected, animated: animated)
        self.accessoryType = selected ? .checkmark : .none
    }
}
class ViewController: UIViewController,UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource  {
    // These strings will be the data for the table view cells
    let sectionNames = ["pizza", "deep dish pizza", "calzone"]
    let data = [["Margarita", "BBQ Chicken", "Pepperoni"],
                ["sausage", "meat lovers", "veggie lovers"],
                ["sausage", "chicken pesto", "prawns", "mushrooms"]]
   //var filteredData: [[String]]!
    let cellReuseIdentifier = "cell"
    var searchController : UISearchController!
    @IBOutlet var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        //filteredData = data
        self.tableView.rowHeight = 78.5
    }

    @IBAction func searchAction(_ sender: Any) {
        // Create the search controller and specify that it should present its results in this same view
        searchController = UISearchController(searchResultsController: nil)
        // Set any properties (in this case, don't hide the nav bar and don't show the emoji keyboard option)
        searchController.hidesNavigationBarDuringPresentation = false
        searchController.searchBar.keyboardType = UIKeyboardType.asciiCapable
        searchController.dimsBackgroundDuringPresentation = false
        searchController.searchBar.barTintColor = #colorLiteral(red: 0.3118804693, green: 0.435135603, blue: 0.9917090535, alpha: 1)
        searchController.searchBar.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        // Make this class the delegate and present the search
        self.searchController.searchBar.delegate = self
        searchController.searchResultsUpdater = self
        present(searchController, animated: true, completion: nil)
    }

    // MARk: - Tableview Delegates
    func numberOfSections(in tableView: UITableView) -> Int {
        return data.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data[section].count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
        cell.myCellLabel.text = data[indexPath.section][indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return sectionNames[section]
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let selectedValue = data[indexPath.section][indexPath.row]
        //send selectedValue to server...
    }
    func updateSearchResults(for searchController: UISearchController) { // SEARCH NOT WORKING
        if let searchText = searchController.searchBar.text {
            filteredData = searchText.isEmpty ? data : data.filter({(dataString: String) -> Bool in
                return dataString.range(of: searchText, options: .caseInsensitive) != nil
            })
            tableView.reloadData()
        }
    }
}

尝试一下 -

将此行添加到其评论的位置。

var filteredData: [[String]] = data

然后更改您的UITATION VIEWDATASOURCE方法,以使用filteredData,而不是data。例如

func numberOfSections(in tableView: UITableView) -> Int {
    return filteredData.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return filteredData[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
    cell.myCellLabel.text = filteredData[indexPath.section][indexPath.row]
    return cell
}

data仅应在搜索代表函数中使用。

我建议您为您的模型采用这些结构

struct MyData {
    var value: String?
    var selected: Bool = false
}
struct tableData{
    var sectionName: String
    var data: [MyData]
}

这些结构将在细胞中同时保存截面名称和选择。在您的班级中,您的模型是:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {
    var searchController : UISearchController!
    var rowNames = [tableData]()
    var filteredData: [tableData]!
    let cellReuseIdentifier = "cell"
    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        //filteredData = rowNames
    }

    //MARK: List Tableview
    func numberOfSections(in tableView: UITableView) -> Int {
        return rowNames.count
    }
     func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return rowNames[section].sectionName
}

由于您正在重复使用相同的tableView进行搜索,因此必须检查搜索栏有效的数据源使用,但尚无输入,请不重新加载数据

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if searchController.searchBar.text != nil && searchController.searchBar.text != ""{
            return filteredData.count
        }else{
            return rowNames.count
        }
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:CustomCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! CustomCell
        if searchController.searchBar.text != nil && searchController.searchBar.text != ""{
             cell.NameLabel.text = filteredData[indexPath.section].data[indexPath.row].value
        }else{
            cell.NameLabel.text =  rowNames[indexPath.section].data[indexPath.row].value
            cell.accesotytype = rowNames[indexPath.section].data[indexPath.row].selected ? .checkmark : .none
        }
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath)
        if searchController.searchBar.text == nil || searchController.searchBar.text == ""{
            if rowNames[indexPath.section].data[indexPath.row].selected == true{
                cell?.accessoryType = .none
                rowNames[indexPath.section].data[indexPath.row].selected = false
            }else{
                cell?.accessoryType = .checkmark
                rowNames[indexPath.section].data[indexPath.row].selected = true
            }
        }
        //self.tableView.deselectRow(at: indexPath, animated: true)
    }
    //MARK: Search Delegate
    func updateSearchResults(for searchController: UISearchController) {
        if let searchText = searchController.searchBar.text, searchController.searchBar.text != "" {
            //maybe betters and faster way to make the search, even not keep the section name in the search IDK but that's unto you
            for row in rowNames{
             let value = row.data.filter({
                    $0.value!.contains(searchText)
                }).map({$0})
                let val = tableData(sectionName: row.sectionName, data: value)
                filteredData.append(val)
            }
            self.tableView.reloadData()
        }
    }
}

pd:这里缺少一件事,如何在标题上tapin时选择所有行,但是您应该提出的另一个问题。

最新更新