Alexander Khitev
Alexander Khitev

Reputation: 6851

UISearchController doesn't find first four data

I have an application with Parse.com. I use UISearchController in parse data. It works is good. But UISearchController doesn't find first four data in parse data. I deleted all data in parse and loaded new data again but the error is there.

Example: I searched iPhone Original in parse data but I didn't find. But I can find other data. Please help me.

enter image description here enter image description here

UPDATED: My code for UISearchController

func updateSearchResultsForSearchController(searchController: UISearchController) {
        self.dataSearchResults?.removeAll(keepCapacity: false)
        var searchText = searchController.searchBar.text
        var query: PFQuery = PFQuery(className: "NewsNow")
        if searchController.active == true  {
            query.whereKey("nameNews", matchesRegex: searchText, modifiers: "i")
            self.tableView.reloadData() // 4 win!!!
        }
        query.findObjectsInBackgroundWithBlock { (results:[AnyObject]?, error: NSError?) -> Void in
            self.dataSearchResults = results as? [PFObject]
            self.tableView.reloadData()
        }
         println(searchText)
    }

    func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
        updateSearchResultsForSearchController(searchController)
    }
 func didDismissSearchController(searchController: UISearchController) {
        self.dataSearchResults = nil //nil
        self.tableView.reloadData()
    }

Upvotes: 0

Views: 61

Answers (1)

ntoonio
ntoonio

Reputation: 3133

I don't know how to fix you problem. But I do have the code I use for searching in UITableView. I'm sure you can make this work for your need!

@IBOutlet var tableView:UITableView!
@IBOutlet var searchBarObj:UISearchBar!

var is_searching:Bool!
var dataArray:NSMutableArray!
var searchingDataArray:NSMutableArray!

override func viewDidLoad() {
    is_searching = false
    dataArray = ["Apple", "Samsung", "iPHone", "iPad", "Macbook", "iMac" , "Mac Mini"]
    searchingDataArray = []
    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    if is_searching == true{
        return searchingDataArray.count
    }else{
        return dataArray.count
    }
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
    if is_searching == true{
        cell.textLabel?.text = searchingDataArray[indexPath.row] as NSString
    }else{
        cell.textLabel?.text = dataArray[indexPath.row] as NSString
    }
    return cell
}

func searchBar(searchBar: UISearchBar, textDidChange searchText: String){
    if searchBar.text.isEmpty{
        is_searching = false
        tableView.reloadData()
    } else {
        println(" search text %@ ",searchBar.text as NSString)
        is_searching = true
        searchingDataArray.removeAllObjects()
        for var index = 0; index < dataArray.count; index++
        {
            var currentString = dataArray.objectAtIndex(index) as String
            if currentString.lowercaseString.rangeOfString(searchText.lowercaseString)  != nil {
                searchingDataArray.addObject(currentString)

            }
        }
        tableView.reloadData()
    }
}

Upvotes: 1

Related Questions