Clear previous search result in search controller in swift - ios

I initiate search after a user enter 3 characters in search bar with following command. It works properly.
if searchController.searchBar.text?.characters.count > 2 { ... }
But, if user delete the search text that entered before and write another one, the table view starts to display previous results until search text reaches 3 characters.
So, how can I clear previous appended search results from memory ?

Related

swift search contents of local HTML files via search bar and display results in tableview

I have a very simple iOS swift app (storyboards) that displays a tableView controller to show a list of items, let's say in this case a list of fruits (Bananas, Cherries, Strawberries, Grapes, Pears).
Tapping on any table cell opens another viewController, loading a webView which displays a local HTML page (located in a directory within the app) of formatted content with respect to that item. So if you tape on "Bananas", it will load a local HTML file with a ton of formatted content regarding Bananas.
I am trying to add a search bar to the tableView controller to allow me to search the contents all of the HTML files within the app for a particular string, and returning a filtered tableView list including only those results that match the string appearing in the end HTML files.
For example, the HTML pages for "Cherries" and "Strawberries" contain the word "red," so if I search for "red" via the search bar, the tableView would filter and only show "Cherries" and "Strawberries" because only those pages have the search term in their respective HTML contents.
Basically, I am trying to make a very simple search engine within the app.
There are a ton of tutorials about how to filter a tableView based on search bar, but those only talk about filtering by the Label, not by the end content.
Is something like this even possible? I don't know where to start.
Trying to avoid databases or anything too complicated as it is a very simple app that just needs a basic content search feature.

State change when back button click

I'm implementing search function on my project. I have three text boxes and a search button
Fist Name
Second Name
Age
Search button generate search on another result page. The problem is that, when I click back button from the result page, Then the Age text box filled with first Name data which I enter previously and first name field remains empty. How did it happen.

IOS: UITableView with search: displayController not selectable?

I'm a newbie trying to create a simple sample app that takes json data from a website and list it on a UITableView.
Each voice in the UITableView is clickable to show more details.
I started from this example/app because it was clean and working:
http://nscookbook.com/2013/12/ios-programming-recipe-16-2-populating-a-uitableview-with-data-from-the-web-ios-7-and-afnetworking-2-0/
What I was missing was a searching function, so following this other page and adapting code I got it working:
http://www.appcoda.com/search-bar-tutorial-ios7/
The only problem I have is that when I search for something I get correct results filtered, but the list of results it's not clickable (to go to the details view/page) and it does not contain the small 'Rating' subtext.
Basically the filtered list only gets the 'Name' and nothing else of the original array, and the cells are not clickable.
What am I missing?
I solved eventually the issue using a SearchBar without its own display view, so basically refreshing the main tableview (already configured to show all details) if/when something is searched.
The search simply filter the main array and in all functions to display the result I show either the main array or the filtered one.
Thanks!

Populating UISearchBar with String from PrepareForSegue with CoreData/NSFetchedResultsController

I have a simple application which is a table view (TV1) that gets populated by the user tapping the plus button in the navigation bar. The user gets taken to an Add Entry screen where tapping on the "name" text field brings up another Table View (TV2) for the user to add existing entries, search for existing entries or add a new entry. This works really well and delegates ensures that what I select in the new Table view (TV2) populates the Name Text Field.
What I want to achieve is the other way around also; if a user has the name text field already populated and clicks on it to bring up the TV2, I want the search bar to already be populated with the string that was passed through while ensuring the searching still works.
For example, if I have the nameTextField with John, when I click on the nameTextField and get taken to the TV2, I want the searchBar.text to say John, but also for the searching to now only show entries with John in the name, just like it would be if I start typing the name John.
I am using Core Data and NSFetchedResultsController.
In the prepaeForSegue of the Add Entry, I am doing this:
if ([self.nameTextField.text length] > 0)
{
[envylopeNameTextFieldTableViewController setSelectedName:self.nameTextField.text];
}
In the TV2, the setSelectedName method is:
- (void)setSelectedName:(NSString *)selectedName
{
_selectedName = selectedName;
self.nameAddSearchBar.text = selectedName;
[self searchBar:self.nameAddSearchBar textDidChange:selectedName];
}
The last line in there is a trial by me because that method is what gets called when I start searching, so I thought I could call that. I tried it without it and with self.nameAddSearchBar.text = self.selectedName and _selectedName and nothing; the searchBar does not get populated.
With a breakpoint, I can see that selectedName = John and _selectedName = John, but self.nameAddSearchBar.text = nil.
I can imagine I'm one or two lines of code away, but I'm just not sure how to proceed.
Any assistance would be really great.
Thanks,
Try moving
self.nameAddSearchBar.text = selectedName
from the setter (-setSelectedName) into viewDidLoad in TV2. The issue here is that you're trying to assign nameAddSearchBar (a view) before it has loaded.
Since the search bar is part of the "view", it is not be available until "viewDidLoad". You can set the selectedName property with the right value and then update the view (search bar.txt) with that value once the view is loaded (in viewDidLoad).

Switch off auto-suggestions on a SearchBar

I'm using a SearchBar, and after at least three characters are entered, I show a TableView list with possible words from a dictionary. The user can tap on a word in this list to select the desired word.
This works fine as long as iOS doesn't show his own suggestion (shown in blue below the input field).
Example: I type in "autof", and the TableView show me two entries (in German 1. "Auto fahren" and 2. "Autofahrer").
iOS show me the word "autofocus" in blue with a "X" to delete it.
If I select the second entry in this state, I want to search for "Autofahrer", but the delegation method tell me, the search word has changed to "autofocus". Now, before processing the TableView selection, a new search for possible words for "autofocus" is made, resulting in an empty TableView list, and after this, the prior selection with index = 1 is made. Of course, this results in a crash on an empty list!
I not want the iOS suggestions at all, only the selection from the list. How can I tell my program to not show suggestions on my SearchBar?
Set autocorrectionType = UITextAutocorrectionTypeNo for a UITextField or UITextView as these both implement the UITextInputTraits protocol. The UISearchBar implements an UITextField for you and exposes this property.

Resources