Hi, I am trying to use the new UISearchController for IOS 8 and i have been running into problems. I have it working but the problem is that i have created a custom cell which includes a title name and an image. I had created two arrays, one for the titles (var Recipes) and one for the images (var recipePhotos). It all worked ok but when the search is active it only filters the title and never changes the image and only keeps the peach and cocnut image.
Since then i have now created a new class named RecipeData so that i can assign an image and a title to each other, now the current problem is that the filtering method ive used says that it doesnt NSArray doesnt have a member named filter.
Any ideas or different methods on how i can get this filtering to work with my custom cells.
im using swift too
Any advice would be so helpful.
Thanks in advance
Alex
class RecipeListViewController : UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate {
@IBOutlet weak var tableView: UITableView!
var Recipes = ["Peach And Coconut Muffins","Vegan Bolognese"]
var recipePhotos = ["Peach And Coconut Muffins.jpg","Vegan Bolognese.jpg"]
var Recipe = ""
var filteredRecipes : [String]!
var searchController : UISearchController!
let kCellIdentifier: String = "recipeCell"
var searchActive : Bool = false
var tintColor : UIColor?
var placeHolder : String?
var recipe1 = RecipeData()
var recipe2 = RecipeData()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
self.filteredRecipes = Recipes as! [String]
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.Plain, target:nil, action:nil)
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.searchBarStyle = .Minimal
searchController.searchBar.sizeToFit()
searchController.searchBar.tintColor = UIColor.whiteColor()
searchController.searchBar.placeholder = "Search For Recipes"
tableView.tableHeaderView = searchController.searchBar
definesPresentationContext = true
var textFieldInsideSearchBar = searchController.searchBar.valueForKey("searchField") as! UITextField
textFieldInsideSearchBar.textColor = UIColor.whiteColor()
recipe1.name = "Peach And Coconut Muffins"
recipe1.imageName = "Peach And Coconut Muffins.jpg"
recipe2.name = "Vegan Bolognese"
recipe2.imageName = "Vegan Bolognese.jpg"
self.Recipes = [recipe1, recipe2]
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchText = [searchController.searchBar.text , searchController.searchBar. ]
filteredRecipes = searchText.isEmpty ? Recipes : Recipes.filter NSArray does not have a member named filter!
({(dataString: String) -> Bool in
return dataString.rangeOfString(searchText, options: .CaseInsensitiveSearch) != nil
})
tableView.reloadData()
}