2

Forgive me for not being able to figure this out on my own. I picked up iOS at Swift, and didn't learn much of objective-c.

I'm attempting to build in a uisearchbar that filters through a grouped table view composed of multiple arrays for it's sections. Essentially, I need to filter through an NSMutableArray built of other arrays in order to get my search results (normal array couldn't compile in 'reasonable time'). I took a look at this tutorial and it's getting at what I need, but unfortunately, I'm not the best when it comes to translating Obj-c over to swift, in this case. Any help (translation) or direction to other tutorials that will help me do this is greatly appreciated. I've been searching and searching, and tinkering all night -- but I've caved, I need some help.

Edit: still not answered

Community
  • 1
  • 1
Ryan Daulton
  • 1,159
  • 1
  • 11
  • 22

2 Answers2

1

It is similar to objective-c code but lets start from here

After you create your view controller,table view and search bar components, In ViewController class, you have to delegate items to ViewController like:

tableView.delegate = self
tableView.dataSource = self
searchBar.delegate = self

And then you have to implement(override) methods like

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

    filtered = data.filter({ (text) -> Bool in
        let tmp: NSString = text
        let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
        return range.location != NSNotFound
    })
    if(filtered.count == 0){
        searchActive = false;
    } else {
        searchActive = true;
    }
    self.tableView.reloadData()
}

explained here Delegation

also here's a reference-video Youtube

hope it helps

Özgür Ersil
  • 6,909
  • 3
  • 19
  • 29
  • This doesn't answer the question I'm asking :/ I'm trying to filter through an `NSMutableArray` built of *other* arrays. Not simply `.filter` through a simple array. That I can do. – Ryan Daulton May 18 '15 at 20:34
0

Add these UISearchBarDelegate methods in your class and your class must confirm to this protocol using either code self.yourSearchBar.delegate = self or in storyboard.

   func searchBarTextDidEndEditing(searchBar: UISearchBar) {
    searchBar.resignFirstResponder()
}


func searchBarCancelButtonClicked(searchBar: UISearchBar) {

    searchBar.text = ""
    searchBar.resignFirstResponder()
    self.searchedArrayToDisplay = self.listDataArray
}


func searchBarSearchButtonClicked(searchBar: UISearchBar) {

    searchBar.resignFirstResponder()
}


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

    if searchText == "\n" {

        searchBar.resignFirstResponder()
    } else if searchText == "" {

    self.searchedArrayToDisplay = self.listDataArray
    } else {

        self.filterContentForSearchText(searchText)
    }
}


func filterContentForSearchText(searchText: String) {

    self.searchedArrayToDisplay = self.listDataArray.filter({(eachObject: [String:AnyObject]) -> Bool in

        let name = eachObject["name"] as! String
        let stringMatch = name.lowercaseString.rangeOfString(searchText.lowercaseString)
        let result = stringMatch != nil ? true : false
        return result
    })
}

Now in cell for at index path method

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {

        let cell : UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Value1, reuseIdentifier:"cell")
        var object = self.searchedArrayToDisplay[indexPath.row]
}
Amit89
  • 3,000
  • 1
  • 19
  • 27
  • Ok this is making sense, however, you cannot use `.filter()` on an `NSMutableArray` as you have in `filterContentForSearchText`. My "`listdataArray`", as you've put it, is an NSMutableArray. That command is not a member. I believe you have to use `NSPredicate` or some variation?? – Ryan Daulton May 18 '15 at 19:47
  • Also, I tried simple casting the `NSMutableArray` as a normal array in order to use the `.filter`. But I'm getting an error: 'Cannot invoke 'filter' with an argument list of type '(([String : AnyObject]) -> Bool)' ...any suggestions for either of these? – Ryan Daulton May 18 '15 at 20:32
  • This one i did using Swift Array. if you are using NSMutableArray, you need to use some other logic. By the way why not you are using swift array. Its easy. – Amit89 May 19 '15 at 04:59
  • Because a swift array could not compile within reasonable time. It's a very large array of data. Any idea how to go about the logic for doing this with `NSMutableArray`? – Ryan Daulton May 19 '15 at 05:25
  • let predicate = NSPredicate(format: "name contains [c] %@", argumentArray: [searchText]) self.listDataArray.filterUsingPredicate(predicate) Something like this you need to do. – Amit89 May 19 '15 at 05:37
  • This will not work with bool. I just gave you the predicate.If you want i can give the full code. – Amit89 May 19 '15 at 16:37
  • I would be very grateful for the full code. I'm completely stuck here – Ryan Daulton May 19 '15 at 16:51
  • Hello @Amit89, could you still provide the code for this? – Ryan Daulton May 26 '15 at 16:41