如何拥有一个显示不同 uitableview 建议的搜索栏 [英] How to have a searchbar which shows suggestions with different uitableview

查看:22
本文介绍了如何拥有一个显示不同 uitableview 建议的搜索栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在导航栏中有一个搜索栏,当用户开始输入时,显示一些建议,但另外 uitableview 必须与搜索结果不同.例如:用户开始输入wh":然后显示一个包含 white、wheater、who、...的列表.然后当按下搜索按钮时会显示其他带有结果的列表.

I want to have a search bar at navigation bar and when the user start typing, show some suggestions, but in additional the uitableview has to be different than the results of search. For example: User starts typing 'wh': then shows a list with white, wheater, who, ... And then when press search button shows other list with the results.

在这种情况下, uitableviewcell 会变得复杂,因为它们是具有不同字段的不同单元格.

In this case the complication goes on uitableviewcell, because they are different cells with different fields.

推荐答案

我在寻找同样的问题时看到了您的问题.现在我已经学会了如何在不同的 tableview 中显示 google 建议和自动完成.我相信你已经完成了,但我会回答,以防其他人需要它.

I saw your question while i was looking for the same thing. Now i have learned how to show google suggestions and autocomplete in different tableview. I am sure you have already accomplished it but i will answer anyway in case someone else needs it.

首先,当 searchBar textfieldeditingDidBegin 方法被调用时,你需要插入 tableview 来查看这样的视图:

First you need to insert tableview to view like this when searchBar textfield editingDidBegin method get called:

    func showSuggestionsTableView() {
        if suggestionsTableView == nil {
            //I get keyboardhight dynamically and 60 is my navigationBar height.
            let availHeight = Globals.deviceScreenSize!.size.height - 60 - CGFloat(keyboardHeight)
            suggestionsTableView = UITableView(frame: CGRect(x: 0, y: 82, width: Globals.deviceScreenSize!.size.width, height: availHeight), style: .grouped)
            suggestionsTableView?.delegate = self
            suggestionsTableView?.dataSource = self

            self.view.insertSubview(suggestionsTableView!, aboveSubview: webViewContainer)
        suggestionsTableView?.isHidden = false
    }

当用户完成搜索时我如何从视图中删除 tableview

How i remove tableview from view when user is done with search

  func removeSuggestionsTableView() {
        suggestionsTableView?.removeFromSuperview()
        suggestionsTableView = nil
    }

我创建了建议管理器来获取建议和自动完成数据.创建与其他类通信的协议:比创建您的管理器类,不要忘记调用 XMLParserDelegate.您必须解析来自 google 的 XML 数据.

Than i created suggestions manager to get suggestion and autocomplete data. Create protocol to communicate with other classes: Than create your manager class, don't forget to call XMLParserDelegate. You have to parse XML data coming from google.

protocol GoogleAutoComplateManagerDelegate {
    func didDownloadResults(resultArr: [String]?)
    func didFail(String)
}

class GoogleAutoComplateManager : NSObject, XMLParserDelegate {

var delegate : GoogleAutoComplateManagerDelegate?
static let sharedInstance = GoogleAutoComplateManager()

var parser = XMLParser()
var resultArr = [String]()



func getAutoComplateResults(stringToSearch: String) {
    if stringToSearch != "" {
        resultArr = []
        //You can find about this google url parameters online. For now 'hl' is language parameter.
        let googleURL = "http://suggestqueries.google.com/complete/search?output=toolbar&hl=tr&ie=utf8&oe=utf8&q="
        let searchURL = URL(string: googleURL + stringToSearch.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)

        parser = XMLParser(contentsOf: searchURL!)!
        self.parser.delegate = self

        let success:Bool = self.parser.parse()

        if success {
            delegate?.didDownloadResults(resultArr: resultArr)
        }
        else {
            delegate?.didFail("parser error")
        }

    }
}


func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
    //This delegate method loops through every suggestion in xml file and parses it
    if (elementName == "suggestion") {
        let suggestion : String = attributeDict["data"]!
        resultArr.append(suggestion)
    }


 }

    func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {
        //Parser delegate method for error handling while parsing
        delegate?.didFail(parseError.localizedDescription)
    }

}

现在你可以像这样从任何地方打电话给你的班级;

Now you can call your class from anywhere like this;

GoogleAutoComplateManager.sharedInstance.delegate = self
GoogleAutoComplateManager.sharedInstance.getAutoComplateResults(stringToSearch: yourSearchString)

不要忘记实现委托方法.

Don't forget to implement delegate methods.

希望对某人有所帮助.

干杯.

这篇关于如何拥有一个显示不同 uitableview 建议的搜索栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆