【问题标题】:Swift - Search Bar Initial LoadSwift - 搜索栏初始加载
【发布时间】:2016-01-13 21:52:09
【问题描述】:

我正在尝试在我的应用程序中添加搜索功能。搜索基本上会在后端的 Parse 类中搜索用户输入。

到目前为止,下面的代码非常适合我的情况,只是视图加载后(在开始输入任何内容之前)它会将后端中的所有用户名加载到表格行中。当用户键入字母时,它会过滤。我希望拥有所有相同的功能,除了在视图加载后立即在表行中显示所有用户。如何实现?

class FriendByUsernameTableViewController: UITableViewController, UISearchBarDelegate, UISearchDisplayDelegate {

    var friendObject = FriendClass()

    @IBOutlet weak var searchBar: UISearchBar!

        var searchActive : Bool = false
    var data:[PFObject]!
    var filtered:[PFObject]!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.searchBar.delegate = self
        search()
    }

    func search(searchText: String? = nil){

        let query = PFQuery(className: "_User")
        if(searchText != nil){
            query.whereKey("appUsername", containsString: searchText)
        }
        query.findObjectsInBackgroundWithBlock { (results, error) -> Void in
            self.data = results as? [PFObject]!
            self.tableView.reloadData()
        }

    }


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

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if(self.data != nil){
            return self.data.count
        }
        return 0
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell


        let obj = self.data[indexPath.row]
        cell.textLabel!.text = obj["appUsername"] as? String


        return cell
    }

    func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
        searchActive = true;
    }

    func searchBarTextDidEndEditing(searchBar: UISearchBar) {
        searchActive = false;
    }

    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
        searchActive = false;
    }

    func searchBarSearchButtonClicked(searchBar: UISearchBar) {
        searchActive = false;
    }

    func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
        search(searchText)
    }


}

代码基于本教程:http://shrikar.com/parse-search-in-ios-8-with-swift/

【问题讨论】:

    标签: ios xcode swift parse-platform


    【解决方案1】:

    尝试添加

    if searchText!.stringByTrimmingCharactersInSet(.whitespaceCharacterSet()) != "" {
        query.findObjectsInBackgroundWithBlock()
    }
    

    在你的搜索功能中当然还有块。

    这样,如果你点击搜索栏或点击一堆空格,你就不会查询。

    另外,如果您不希望用户在加载时显示,请不要在您的 viewDidLoad() 中调用 search()

    【讨论】:

    • 这并不能解决手头的问题。问题是视图加载的结果已经显示在我希望它只在搜索字段收到用户名时显示结果的位置
    • @ksa_coder 查看我的编辑。通过在viewDidLoad() 中调用search(),您正在运行查询并使用对象加载您的数组,而不是将用户名与searchText 进行比较,因此您的tableView 将显示您的用户类中的所有用户。
    • 嗨。抱歉迟了回应。尝试了上面的代码并导致错误:致命错误:在展开可选值时意外发现 nil
    • 我发布的代码是为了让您在不输入非空白字符的情况下不会得到结果。我误解了,我强制展开 searchText,所以你必须确保检查它是否不是 nil,如果你不想在视图加载时得到结果,请不要在 viewDidLoad() 中查询。在 searchBarTextDidBeginEditing() 中尝试您的查询
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多