ios - 没有在自定义 UISearchBar 中获取过滤器数据

标签 ios swift uitableview swift3 uisearchbar

我正在使用 swift 3 开发一个项目,我有一个特定的 UIViewController,我有一个 UITableView 并在其单元格上填充数据,数据是从服务器获取的,我将它分配给一个类型的数组JSON。我想实现一个自定义 UISearchBar 来过滤这些数据。我已经部分地研究了代码,如下所示。

import UIKit
import SwiftyJSON
import SDWebImage


class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating,UISearchBarDelegate {    

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var searchBar: UISearchBar!    
    var searchActive : Bool?
    var selectedCategoryList = [JSON?]()
    var filterSelectedCategoryList = [JSON?]()
    var lab :String?
    let searchController = UISearchController(searchResultsController: nil)
    override func viewDidLoad() {
    super.viewDidLoad()
            passJson()

     self.searchBar.delegate = self
            self.tableView.reloadData()
    }

            func updateSearchResults(for searchController: UISearchController) {
    }
        func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
            filterSelectedCategoryList = selectedCategoryList.filter { titles in
                return (titles?["healerName"].stringValue.lowercased().contains(searchText.lowercased()))!
            }
            tableView.reloadData()
            print("Array : ", filterSelectedCategoryList)
        }

     func passJson() {
            let path : String = Bundle.main.path(forResource: "JsonFile", ofType: "json") as String!

            let jsonData = NSData(contentsOfFile: path) as NSData!

            let readableJson = JSON(data: jsonData as! Data, options: JSONSerialization.ReadingOptions.mutableContainers, error: nil)

            let json = readableJson

             print(json)

            let selectedItemArray = json["itemList"].arrayValue
            self.selectedCategoryList = selectedItemArray
            print("new prints",self.selectedCategoryList)
            tableView.reloadData()
            // print( "Count",selectedItemArray?.count as Any)


            self.count = self.selectedCategoryList.count
            print(self.count)


        }


        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }


        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            if self.searchController.isActive && self.searchBar.text != ""{
                return filterSelectedCategoryList.count
            }else{
                return selectedCategoryList.count
            }

        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell
            if count > 0 {
                if self.searchController.isActive && self.searchBar.text != ""{

                    cell.label.text = filterSelectedCategoryList[indexPath.row]?["healerName"].stringValue


                }else{
                    cell.label.text = selectedCategoryList[indexPath.row]?["healerName"].stringValue
                    cell.textLabel?.text = selectedCategoryList[indexPath.row]?["id"].stringValue
                    //cell.textLabel?.text=
                    self.lab = cell.textLabel?.text

                    cell.textLabel?.isHidden = true
                }


            }

            return cell
        }

最佳答案

从您的代码中,您使用的是 UISearchBar 而不是 UISearchController,因此您只需比较 numberOfRowsInSection 方法中的 searchBar 文本 并且在 cellForRowAt 中,不要将 searchController.isActive 与它进行比较。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if self.searchBar.text != ""{
        return filterSelectedCategoryList.count
    }else{
        return selectedCategoryList.count
    }        
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell
    if self.searchBar.text != ""{
        cell.label.text = filterSelectedCategoryList[indexPath.row]?["healerName"].stringValue

    }else{
        cell.label.text = selectedCategoryList[indexPath.row]?["healerName"].stringValue
        cell.textLabel?.text = selectedCategoryList[indexPath.row]?["id"].stringValue
        //cell.textLabel?.text=
        self.lab = cell.textLabel?.text

        cell.textLabel?.isHidden = true
    }
    return cell
}

关于ios - 没有在自定义 UISearchBar 中获取过滤器数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42900497/

相关文章:

iOS:在 UIViewRepresentable 和 View 之间进行函数调用,SwiftUI

iphone - 反转 UIWebView 或 UITextView 中的颜色

ios - 在 iOS 中使用 google map SDK 将矩形变成相等的网格

ios - didSelectRowAtIndexPath 只被调用一次

objective-c - 将 NSDictionary 附加到其他 NSDictionary

iOS - UITableView 不显示单元格

ios - iOS:如何正确添加运行脚本?

iphone - setSelected 上的自定义 UITableViewCell 和动画 :animated:

ios - webViewDidFinishLoad() 似乎没有在 Swift 中运行

xcode - 试图在 swift 中为约束设置动画