ios - 如何使用搜索栏使用 swift 从 json 结果中获取数据?

标签 ios json swift2 tableview searchbar

要求:我需要用UISearchBar过滤UITableView中的JSON数据,所以我放置了 UISearchBar(不是 UISearchBarController)到我的 View Controller 的顶部,我将 UITableView 放在 UISearchBar 下面,我也有 api key 其中包含 json 格式的数据。

我的 View Controller 中的代码:

 class FourthViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,UITabBarControllerDelegate,UISearchDisplayDelegate{
   var arrDict = NSMutableArray()
   var FilteredData = NSMutableArray()
    var userid:String!
    @IBOutlet var SearchButton: UISearchBar!
    @IBOutlet var SlideShow: ImageSlideshow!
    @IBOutlet var MyTableView: UITableView!
    @IBOutlet var PostButton: UIButton!

    override func viewDidLoad() {
    super.viewDidLoad()
     self.navigationController?.navigationBar.hidden = true
     SearchButton.delegate = self

    jsonParsingFromURL()
        SlideShow.backgroundColor = UIColor.whiteColor()
        SlideShow.slideshowInterval = 5.0
        SlideShow.pageControlPosition = PageControlPosition.UnderScrollView
        SlideShow.pageControl.currentPageIndicatorTintColor = UIColor.lightGrayColor()
        SlideShow.pageControl.pageIndicatorTintColor = UIColor.blackColor()
        SlideShow.contentScaleMode = UIViewContentMode.ScaleAspectFill
        SlideShow.setImageInputs(alamofireSource)
           }
func jsonParsingFromURL () {
    if Reachability.isConnectedToNetwork() == true
    {
    Alamofire.request(.GET, "http://something.com", parameters: nil, encoding: .URL, headers: nil).response { (req, res, data, error) -> Void in
        let dataString = NSString(data: data!, encoding:NSUTF8StringEncoding)
        print(dataString)
        self.startParsing(data!)
    }
    }
    else{
        let alert = UIAlertController(title: "No Internet Connection", message: "make sure your device is connected to the internet", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alert, animated: true, completion: nil)
    }
}

func startParsing(data :NSData)
{
    let dict: NSDictionary!=(try! NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)) as! NSDictionary
    for i in 0  ..< (dict.valueForKey("ads") as! NSArray).count
    {
        arrDict.addObject((dict.valueForKey("ads") as! NSArray) .objectAtIndex(i))
    }
    MyTableView.reloadData()
}

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

          return arrDict.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("FirstCell") as! FirstTableViewCell
    let strTitle : NSString=arrDict[indexPath.row] .valueForKey("categoryname") as! NSString
    let photoImage : NSString=arrDict[indexPath.row] .valueForKey("image1") as! NSString
    let SecondImage : NSString=arrDict[indexPath.row] .valueForKey("image2") as! NSString
     let ThirdImage : NSString=arrDict[indexPath.row] .valueForKey("image3") as! NSString
     let FourthImage : NSString=arrDict[indexPath.row] .valueForKey("image4") as! NSString
    let URL_API_HOST2:String = "https://www.imagestring.com/"
  //  let FourthData = NSData(contentsOfURL: NSURL(string: URL_API_HOST2 + (FourthImage as String))!)
    cell.image1.sd_setImageWithURL(NSURL(string: URL_API_HOST2 + (photoImage as String)))
    cell.image2.sd_setImageWithURL(NSURL(string: URL_API_HOST2 + (SecondImage as String)))
         // cell.image2.image = UIImage(data: SecData!)
    cell.image3.sd_setImageWithURL(NSURL(string: URL_API_HOST2 + (ThirdImage as String)))
    cell.image4.sd_setImageWithURL(NSURL(string: URL_API_HOST2 + (FourthImage as String)))
    cell.CategoryName.text = strTitle as String
    return cell
    }

问题: 我已经加载了一个称为类别的 api key 。现在我需要使用搜索栏获取子类别数据。子类别有另一个 api ....

最佳答案

Apple 声明: UISearchController 对象根据与搜索栏的交互来管理搜索结果的显示。 description here

如果您正在使用 UISearchBar

import UIKit

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

//MARK:- Outlets
//MARK:-
    @IBOutlet var tblSearch: UITableView!

//MARK:- Properties
//MARK:-

    var dataArray = [String]()
    var filteredArray = [String]()
    var shouldShowSearchResults = false
    var searchController: UISearchController!

//MARK:- VDL
//MARK:-
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        loadListOfCountries() // get the data from file
        configureSearchController() // Config Controller in VC
    }

//MARK:- VC Methods
//MARK:-

    func loadListOfCountries() {
        // Specify the path to the countries list file.
        let pathToFile = Bundle.main.path(forResource: "Country", ofType: "txt")

        if let path = pathToFile {
            // Load the file contents as a string.

            do{
                let countriesString = try String(contentsOfFile: path, encoding: String.Encoding.utf8)
                self.dataArray = countriesString.components(separatedBy: "\n")
            }
            catch{
                print("try-catch error is catched!!")
            }
            tblSearch.reloadData()
        }
    }


    func configureSearchController() {
        searchController = UISearchController(searchResultsController: nil)
        searchController.dimsBackgroundDuringPresentation = false
        searchController.searchBar.placeholder = "Search here..."
        searchController.searchBar.delegate = self
        searchController.searchResultsUpdater = self
        searchController.searchBar.sizeToFit()

        self.tblSearch.tableHeaderView = searchController.searchBar
    }

    //MARK:- table datasource
    //MARK:-

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        if shouldShowSearchResults {
            return filteredArray.count
        }
        else {
            return dataArray.count
        }
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell

        if shouldShowSearchResults {
            cell.textLabel?.text = filteredArray[indexPath.row]
        }
        else {
            cell.textLabel?.text = dataArray[indexPath.row]
        }

        return cell
    }

    //MARK:- search update delegate
    //MARK:-

    public func updateSearchResults(for searchController: UISearchController){
        let searchString = searchController.searchBar.text

        // Filter the data array and get only those countries that match the search text.

        filteredArray = dataArray.filter({ (country) -> Bool in
                    let countryText: NSString = country as NSString
                    return (countryText.range(of: searchString!, options: .caseInsensitive).location) != NSNotFound
                })
        tblSearch.reloadData()
    }

    //MARK:- search bar delegate
    //MARK:-

    public func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        shouldShowSearchResults = true
        tblSearch.reloadData()
    }


    public func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        shouldShowSearchResults = false
        tblSearch.reloadData()
    }

}

如果您正在使用 UITextField

import UIKit

class TDSearchVC: UIViewController ,   UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate{


    @IBOutlet var textSearch: UITextField!
    @IBOutlet var tblSearchResult: UITableView!

    var arrData : [String] = []
    var arrFilterData : [String] = []
    var isSearch : Bool!


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        isSearch = false

        /*
         * If date Data is in Json then use JSON Serialization
         */
        arrData = ["Apple", "Banana", "Chikoo", "Brew", "Cherry", "Mango", "Lotus", "Peacock", "Temple", "Pine Apple","Glass", "Rose", "Church", "Computer", "Carrot"]

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK:- textfield

    public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{

        var searchText  = textField.text! + string

        if string  == "" {
            searchText = (searchText as String).substring(to: searchText.index(before: searchText.endIndex))
        }

        if searchText == "" {
            isSearch = false
            tblSearchResult.reloadData()
        }
        else{
            getSearchArrayContains(searchText)
        }

        return true
    }

    // Predicate to filter data
    func getSearchArrayContains(_ text : String) {
        var predicate : NSPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", text)
        arrFilterData = (arrData as NSArray).filtered(using: predicate) as! [String]
        isSearch = true
        tblSearchResult.reloadData()
    }

    // MARK:- TableView Delegates
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

        if isSearch! {
            return arrFilterData.count
        }
        else{

            return arrData.count
        }
    }

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

        var cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell

        if isSearch! {
            cell.textLabel?.text = arrFilterData[indexPath.row]
        }
        else{

            cell.textLabel?.text = arrData[indexPath.row]
        }
        return cell
    }
}

关于ios - 如何使用搜索栏使用 swift 从 json 结果中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41405975/

相关文章:

ios - Swift 中的线程队列

javascript - JSON 数组转换为 Javascript 数组

PHP:如何解析 JSON 字符串并获取变量?

swift - 使用 http ://对 URL 进行编码

html - Xcode 发现字符串中 HTML 代码的语法错误

ios - 加载 TableView 时项目出错

ios - uitableviewcell 动态标签宽度与 VFL

ios - Swift vs ObjC 初始化过程?

objective-c - 单点触控设备只会崩溃

json - Grails REST Client Builder在处理来自Jersey Web服务的响应时遇到JSON反序列化错误