Searching using UISearchBar - iphone

I have a search bar added to the navigation bar. When the user types a word and clicks the search button (keyboard) I need to display results in the table related to the search.
When I click "Search", I get blank records (it actually goes inside the if condition in the searchBarSearchButtonClicked: method and adds the animal object too). I think there's a problem in
[mutableArray addObject:animal];
...
self.animalArray = mutableArray;
[self.tableView reloadData];
My complete code is as follows.
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[self.mutableArray removeAllObjects];
for (Animal *animal in self.animalArray) {
if ([[animal nameOfAnimal] isEqualToString:[searchBar text]] ) {
[mutableArray addObject:animal];
}
}
self.animalArray = mutableArray;
[self.tableView reloadData];
[searchBar resignFirstResponder];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
Animal *animal = [self.animalArray objectAtIndex:indexPath.row];
cell = [[Cell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
cell.nameofani.text=animal.nameOfAnimal;
cell.oriofani.text=animal.originOfAnimal;
}
return cell;
}

Make sure you implement tableView:numberOfRowsInSection: correctly, returning [self.animalArray count] for the search results table view.

try:
self.animalArray = [mutableArray copy];

Related

How to use UITextField to input information to UITableView?

I am trying to make a ToDo list app and I can't for the life of me seem to figure out how to make the text box's typed text go into a box with a tick box for me to be able to change to completed.
Can anyone help me? I am just getting started in Xcode so please explain why/how it works that way I can learn for the future. Thanks guys!
App Rendering (What I am trying to make)
http://imgur.com/wcNXu0z
Currently in Xcode (What I have so far)
http://imgur.com/PLUVaVg
You need to create an IBAction for add button. So its get called when you click the button and store the entered text in UItextfield in an array and you will use that array to add rows in your table.
For example consider the code :
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
[cell.textLabel setText: [dataArray objectAtIndex:indexPath.row];
return cell;
}
-(IBAction)addCity:(id)sender
{
[tableView beginUpdates];
[dataArray addObject:#"City"];
NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[dataArray count]-1 inSection:1]];
[[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];
[tableView endUpdates];
}
I'd strongly recommend you check out the free Sensible TableView framework. The framework should help you automatically create your text fields and automatically fill/save their data.

How to get text from textView from custom cell iphone

I have a table view with custom cell.On my custom cell, I have a label and textView and I want to get data from textView to save in feedBack button. When I add txtView in my data array, I get custom cell twice. How to remove this issue
- (void)textViewDidEndEditing:(UITextView *)textView
{
FeedbackQuestionDC *feedBack = [dataArray objectAtIndex:textView.tag];
feedBack.FeedbackQuestionDC_Answers=textView.text;
[dataArray addObject:feedBack];
[myTableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"Feed Back";
feedBackCC *cell = (feedBackCC *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
UIViewController *c = [[UIViewController alloc] initWithNibName:#"feedBackCC" bundle:nil];
cell = (feedBackCC *) c.view;
}
cell.textLabel.font = [UIFont boldSystemFontOfSize:15.0];
FeedbackQuestionDC *feedBack = [dataArray objectAtIndex:[indexPath row]];
cell.lblQuestion.text = feedBack.FeedbackQuestionDC_QuestionText;
cell.txtViewAnswer.tag=indexPath.row;
cell.txtViewAnswer.text=feedBack.FeedbackQuestionDC_Answers;
cell.txtViewAnswer.delegate=self;
return cell;
}
First get the cell at particular index and then get view from it and then textview
Use following function to get cell
[table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]
- (void)textViewDidEndEditing:(UITextView *)textView
{
FeedbackQuestionDC *feedBack = [dataArray objectAtIndex:textView.tag];
feedBack.FeedbackQuestionDC_Answers=textView.text;
[dataArray addObject:feedBack]; //REMOVE THIS LINE
[myTableView reloadData];
}
See the code above remove the line I suggested, you don't need to add the object again into array, it has already been updated using the reference of the object from dataArray.
Hope this helps..
// i think it will be usefull for you, try this
- (void)textViewDidEndEditing:(UITextView *)textView
{
feedBackCC *cellsuperView = (feedBackCC *)[textView superview];
nslog(#"%#",cellsuperView.txtViewAnswer.text);
}

How can i use prototype cell in storyboard with search result controller

I have tableview with search result controller when search in search bar get this error indicate that there is no cell and get the below error .How can create my prototype cell in this method CellForRowAtIndexPath
Code :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"HoCell";
Ho *cell;
Ho *item;
if (tableView == self.searchDisplayController.searchResultsTableView) {
if (cell == nil)
{
cell = [[Ho alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"HoCell"];
}
item = [searchResultsController objectAtIndexPath:indexPath];
}
else{
cell = (Ho*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
item = [fetchedResultsController objectAtIndexPath:indexPath];
}
cell.ho.text = item.name;
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"se.png"]];
return cell;
}
Error :
*** Assertion failure in -[UISearchResultsTableView _configureCellForDisplay:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:5471
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
There may be two possibilities Here :
1) You are returning a number larger than your Array Count from
tableView:numberOfRowsInSection:. Don't.
2) One or more of your cell# outlets is not hooked up in your nib, or
is not hooked up to a UITableViewCell (or subclass). Hook them up
properly.
Go through this Ray Wenderlich's Link :
How to Add Search Into a Table View
Check this SO Questions :
1) UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath: Exception
2) ios 5 UISearchDisplayController crash
One More Beautiful Link : Custom Prototype Table Cells and Storyboards Just see this Portion :
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:UYLCountryCellIdentifier];
if (cell == nil)
{
[self.countryCellNib instantiateWithOwner:self options:nil];
cell = self.countryCell;
self.countryCell = nil;
}
// Code omitted to configure the cell...
return cell;
}
Your code seems to be buggy. You check for cell == nil while it is not set to nil initially. It also looks a bit strange that you allocate your cells in that way based on search mode.
Anyways, I would do it different way. Imho the way I do it is almost canonical :) Just use your search result to populate your cells with correct data for each case (search mode and normal mode). In this example, searchResult and dataSource are arrays with strings. I think in real life for you it will be something more complex like array of nsdictionary.
In your view controller:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)_section
{
/* Use correct data array so that in search mode we draw cells correctly. */
NSMutableArray *data = searching ? searchResult : dataSource;
return [data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/* Use correct data array so that in search mode we draw cells correctly. */
NSMutableArray *data = searching ? searchResult : dataSource;
static NSString *CellIdentifier = #"CellId";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomTableViewCell alloc] initWithIdentifier:CellIdentifier] autorelease];
}
/* Note this is only correct for the case that you have one section */
NSString *text = [data objectAtIndex:[indexPath row]]
cell.textLabel.text = text;
/* More setup for the cell. */
return text;
}
And here are delegate methods for search controller and some helpers:
- (void) searchTableView
{
NSString *searchText = searchBar.text;
for (NSString *item in dataSource) {
NSRange range = [item rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (range.length > 0) {
[searchResult addObject:item];
}
}
}
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
searching = NO;
}
- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
searching = NO;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0]
withRowAnimation:UITableViewRowAnimationAutomatic];
[searchResult removeAllObjects];
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchText
{
[searchResult removeAllObjects];
if ([searchText length] > 0) {
searching = YES;
[self searchTableView];
} else {
searching = NO;
}
return YES;
}
Hope this helps.
I have the same issue, here is what is did and it is now working perfectly..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Here is my code BEFORE
//
OPUsersTableViewCell *tableCell = [tableView dequeueReusableCellWithIdentifier:TableCellID];
// Here is my code AFTER
//
OPUsersTableViewCell *tableCell = [self.groupTableView dequeueReusableCellWithIdentifier:TableCellID];
Note:
self.groupTableView is where the prototype cell is...

A grouped cell is showing up in my plain tableView and I don't know why

So I don't know how this is happening, or how to fix it, but I am having an issue of my UISearchDisplayController's plain tableView displaying search results with a grouped cell.
I have a dataSource array with a few names, a tableData array that iterates through the dataSource array and adds any entries that fit the searchText, and then depending on which tableView it is, I reload the tableView with the appropriate dataSource...
Ideas anyone?
Code:
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
int count = 0;
if(aTableView == self.tableView){
count = [userTableData count]==0?1:[userTableData count];
}else{
count = [tableData count];
}
return count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return #"Users";
}
- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
}
if(tView == self.tableView){
if([userTableData count] == 0){
cell.textLabel.text = #"Please select a user";
}else{
cell.textLabel.text = [userTableData objectAtIndex:indexPath.row];
}
}else{
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
}
return cell;
}
And then as the search is entered, matching strings are added to the tableData array
assign tags to the UITableView and hide the grouuped tableView while searching
In the documentation for UISearchDisplayController about the searchResultsTableView property, it says
Discussion:
This method creates a new table view if one does not already exist.
So what you could try is create your own UITableViewwhen you set up the UISDC, explicitly making it plain:
UITableView *searchTableView = [[UITableView alloc] initWithFrame:CGRectZero
style:UITableViewStylePlain];
searchDisplayController.searchResultsTableView = searchTableView;

Adding new rows to UITableViewCell

In my application,I will be displaying only one row on the UITableView initially. I want to increase the rows as user loads the previous row with data(an uiimage, here). As now i'm returning value 1, in numberOfRowsInSection: method, since I don't know how to implement it in the required way. Please help.
My cellForRowAtIndexPath: method is
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = #"CustomCellIdentifier";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CustomCellIdentifier ] autorelease];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
for (id currentObject in nib){
if ([currentObject isKindOfClass:[CustomCell class]]){
cell = (CustomCell *)currentObject;
cell.viewController = self;
break;
}
}
}
if (j<15){
cell.imageView.image = nil;
if (count!=0)
{
#try{
NSInteger row = [indexPath row];
cell.imageView.image = [array objectAtIndex:row];
}
#catch (NSException* ex) {
NSLog(#"doSomethingFancy failed: %#",ex);
}
}
}
cell.showsReorderControl = YES;
return cell;
[array release];
}
that if condition and count is nothing but just for checking the correct functioning of the mutable array, 'array'.
You should read this documentation: http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/TableView_iPhone/Introduction/Introduction.html
Have a look to the related samples as well.
Note: in your code [array release] won't be called since you've got a return statement just before it.
When you reach the cellForRowAtIndexPath at a certain indexPath, it means that row is being displayed. If you reach the last row you can insert a new row into the table. To do that, you just need to increase the count that will get returned by the tableView:numberOfRowsInSection function.
I'm guessing your numberOfRowsInSection function will look something like this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return section==0 ? [array count] : 0;
}
So basically, inside cellForRowAtIndexPath, add the following code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
if([indexPath row]==[array count]-1) {
[array addObject:(NEXT_IMAGE)];
}
return cell;
}
You also need to refresh the table. One way is to just call reloadData on your UITableView, but the preferred way is to call beginUpdates/insertRowsAtIndexPaths/endUpdates on the table so that you can show a nice animation of the row being inserted.