How to Add search bar in table view - ios

Actually i have stored too many data in table view and now I want to found my data easily so I need a search bar. Can any one give me a little solution?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifire = #"CellIdentifire";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifire];
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}

Follow these steps:
Drag and drop search bar on table view header
Set delegate for search bar to your view controller
Use following code to search:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:YES];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
//Remove all objects first.
[self searchTerm:searchText];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
NSLog(#"Cancel clicked");
searchBar.text = #"";
[searchBar resignFirstResponder];
[searchBar setShowsCancelButton:NO];
[self showAllData];// to show all data
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSLog(#"Search Clicked");
[searchBar setShowsCancelButton:NO];
[searchBar resignFirstResponder];
[self searchTerm:searchBar.text];
}
-(void)showAllData {
//load all data in _tableViewDataSourceArray, and reload table
}
-(void)searchTerm : (NSString*)searchText
{
if (searchText == nil) return;
_tableViewDataSourceArray = //YOUR SEARCH LOGIC
[self.tableView reloadData];
}
Edit:
In case you wanna create Search bar with code then, Initialize a search bar and pass it as headerView Of your table view, inside
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
Dont's forget to return height of view in
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

Related

make uitableview cell height decided by the automatic height of the cell content when searching

I have a UITable view and a search bar, the thing is if it's in normal state(non-searching mode) the height of each row is static
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 78.0;
}
And when the state changes to searching the cell height should be decided by the actual content, so how can I tell the table view that?
I try to do this like:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSLog(#"Text change - %d",self.searching);
//Remove all objects first.
[self.searchResult removeAllObjects];
if([searchText length] != 0) {
self.searching = YES;
[self searchTableList];
self.tableView.rowHeight = UITableViewAutomaticDimension;
// try to do it with this, not working
[self.tableView reloadData];
}
else {
self.searching = NO;
}
[self.tableView reloadData];
}
But it didn't work
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;//78.0;
}
try this for row height

UISearch Bar in ios 8

I am getting JSONArray data and show phone numbers in table
I am using UISearchBar in UITableView.
When I am type in search bar it reload data and show type value in first cell.
But i clicked first cell means it show old array value.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (isSearching)
{
cell.textLabel.text = [searchResultArray objectAtIndex:indexPath.row];
}
else
{
cell.textLabel.text = [phoneNoArray objectAtIndex:indexPath.row];
}
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[searchResultArray removeAllObjects];
if([searchText length] != 0)
{
isSearching = YES;
[self searchTableList];
}
else
{
isSearching = NO;
}
[self.tableView reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
tempString = [jsonArray objectAtIndex:indexPath.row];
}
when you set isSearching to yes. Now datasoucre of table has changed. and you have reloaded the data.
and you are supposed to update your datasource methods as well
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (isSearching) {
return [searchResultArray count];
}
else
{
return [phoneNoArray count];
}
}
HTH

Detail view not updating when UITableView rows are re-ordered

I have a UITableView that pushes to a Detail ViewController. In my tableview, my rows are draggable/re-arrangeable. However, when the rows are switched, the path stays the same when selecting the rows and the Detail View is presented. Example: TableView Re-order Pic
If I were to switch the "Milk" row with the "Gym" row, then select "Gym", the detail view would still return the details of the milk row.
How do I fix this so that the path of the rows switch appropriately as they do?
.m :
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self->newArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *theFood = [values objectAtIndex:indexPath.row];
cell.textLabel.text = [theFood valueForKey:#"name"];
cell.detailTextLabel.text = [theFood valueForKey:#"price"];
return cell;
}
- (IBAction) EditTable:(id)sender
{
if(self.editing)
{
[super setEditing:NO animated:NO];
[_myTableView setEditing:NO animated:NO];
// [_myTableView reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Edit"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
[super setEditing:YES animated:YES];
[_myTableView setEditing:YES animated:YES];
// [_myTableView reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Done"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
}
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
if( fromIndexPath == toIndexPath ) {
return;
}
NSDictionary *moveFood = [self->newArray objectAtIndex:fromIndexPath.row];
[self->newArray removeObjectAtIndex:fromIndexPath.row];
[self->newArray insertObject:moveFood atIndex:toIndexPath.row];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"detailSegue"]) {
NSIndexPath *indexPath = [self.myTableView indexPathForSelectedRow];
DetailViewController *detailVC = segue.destinationViewController;
detailVC.detailFood = [values objectAtIndex:indexPath.row];
}
}
When you switch the rows you should also update your data source. Otherwise when tableView's didSelect method is called it would access the wrong object in your data source array.
So in –tableView:moveRowAtIndexPath:toIndexPath: switch the location of the objects that were switched.
Special thanks to #HotLicks for helping me!
The problem was I didn't change my datasource for the detail view from values NSArray to my newArray NSMutableArray after the update in my tableView:moveRowAtIndexPath:toIndexPath: method!

Original UITableView is shown over UITableView of the results using Search Bar

I am adapting my App for support both iOS7 and 6.
I am using a Search Bar over a Table View for filter data to show.
But when my search is filtered, appear the table view with filtered data above the original data table view, so they are overlapped.
Does anyone have any idea?
Thanks in advance.
Attach 2 images:
Without search.
With search (as you can see, table view are overlapped)
Edited:
Adding Data Source methods
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return arregloFiltrado.count;
}else{
return arreglo.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"CeldaLista" owner:self options:nil];
cell = celdaLista;
self.celdaLista = nil;
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
[(UILabel*)[cell viewWithTag:1] setText: /*Name and surname*/];
[(UILabel*)[cell viewWithTag:2] setText: /*Other properties*/];
if ([[arregloFiltrado objectAtIndex:indexPath.row] objectForKey:#"photo"] == nil) {
[(UIImageView*)[cell viewWithTag:4] setImage:/*No image*/];
}
else{
[(UIImageView*)[cell viewWithTag:4] setImage:/*Photo*/];
}
} else {
[(UILabel*)[cell viewWithTag:1] setText:/*Name and surname*/];
[(UILabel*)[cell viewWithTag:2] setText:/*Other properties*/];
if ([[arreglo objectAtIndex:indexPath.row] objectForKey:#"photo"] == nil) {
[(UIImageView*)[cell viewWithTag:4] setImage:/*No image*/];
}
else{
[(UIImageView*)[cell viewWithTag:4] setImage:/*Photo*/];
}
}
return cell;
}
Something more?
If you use a UISearchDisplayController it has it's own UITabelview. So as you begin to search the search display controller superimposes the search interface over the original view controller’s view, as described here: https://developer.apple.com/library/ios/documentation/uikit/reference/UISearchDisplayController_Class/Reference/Reference.html
You may want to set the background colour of the table cells or the entire table view for the table provided by the UISearchDisplayController so that is not transparent.
Alternatively you could filter your original table by implementing a UISearchBar and its associated delegate methods:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
//As the user enters search text filter the array that provides data to the UItableView you wish to filter
}
Investigate the searchbar delegate here: https://developer.apple.com/library/ios/documentation/uikit/reference/UISearchBarDelegate_Protocol/Reference/Reference.html
Thanks
TG
Try this code:
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView{
tableView.backgroundColor = [UIColor whiteColor];//color of search result table
}

popover tableview shows then disappears

On a button click I am showing a popover view which is a tableview.
Everything seems to work and the view shows but then disappears in less than a second.
The tableview contains a list of strings and I set the tablecell to be show checkmark.
I notice that table shows all items checked in the split second it is displayed
Anyone knows what I am missing.
Thanks
Hassan
Code to show the popover
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"WholesalersList"]){
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
WholesalersViewController *vc = segue.destinationViewController;
vc.wholesalers = [appDelegate.wholesalers mutableCopy];
if ([segue isKindOfClass:[UIStoryboardPopoverSegue class]]){
self.wholesalersPopoverController = [(UIStoryboardPopoverSegue *)segue popoverController];
[self.wholesalersPopoverController setPopoverContentSize:CGSizeMake(334, 334)];
//[self.wholesalersPopoverController setDelegate:self];
}
}
}
Code in the popover tableview
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section {
if ([self.wholesalers count] == 0) {
return 1;
}
else {
return [self.wholesalers count];
}
}
#pragma mark Table view delegate
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.wholesalers count] == 0) {
return nil;
}
else {
static NSString *CellIdentifier = #"WholesalerCell";
UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell.
Wholesaler *ws = (Wholesaler *)[self.wholesalers objectAtIndex:indexPath.row];
cell.textLabel.text = ws.name;
return cell;
}
}
- (CGFloat)tableView:(UITableView *)theTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44;
}
- (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)tableView:(UITableView *)theTableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return NO; // The table view should not be re-orderable.
}
Are calling the dismissPopover:Animated anywhere in the code like mistakenly in the IBAction method for the button or you dont have an IBAction method?

Resources