UISearchBar in UITableViewController? - ios

I want to add a SearchBar to my TableView. I just dragged the UISearchBar to the header of an UITableView in IB, and it scrolled with my UITableView.
I changed to use UITableViewController now, and when I drag an UISearchBar in the header
of the UITableView which is provided with the UITableViewController, it doesn't show up at all.
Is there a trick?
Kind regards

You can do it programmatically
UISearchBar *tempSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 0)];
self.searchBar = tempSearchBar;
[tempSearchBar release];
self.searchBar.delegate = self;
[self.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchBar;
Hope this helps.

I get it to work by using two UITableViewDelegate Protocol methods –tableView:viewForHeaderInSection: and –tableView:heightForHeaderInSection: as given below.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
if (section == 0) {
UISearchBar *tempSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 0)];
[tempSearchBar sizeToFit];
return tempSearchBar;
}
return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == 0) {
return 40.0f;
}
return 0.1f;
}
Hope it helps.

Whenever I define the searchbar in the nib, I dont add it to the table view's header on purpose. Instead I set it in the viewDidLoad function. Retterdesdialogs solution also works, so not sure why he hasn't got more votes.
- (void)viewDidLoad
{
[super viewDidLoad];
tableView.tableHeaderView = searchBar;
}

It also depends on how to initialized the tableview controller, if you alloc and init via initWithNib:Bundle method it should still show up. But with the tableview controller I think the default implementation method is initWithStyle. If you alloc'd and init with initWithStyle the nib file will never load.

I just tried it and it worked for me, there shouldn't be a trick. If you want to post a distilled version of your project somewhere I can help you look.

Related

Tableview is not visible on UIViewController

UITableView is not visible on the UIViewController Class.
On Storyboard added a new UIViewController set to related SlideViewcontroller class to it, Add a new UITableView (tbl_Slide), set the datasource and delegate to UIViewController in storyboard for the added UITableView.
And also included the related datasource and delegate methods to the UIViewController class.
- (void)viewDidLoad {
[super viewDidLoad];
self.tbl_SlideMenu = [[UITableView alloc]
initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
self.tbl_SlideMenu.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
self.tbl_SlideMenu.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.tbl_SlideMenu.separatorColor = [UIColor lightGrayColor];
self.tbl_SlideMenu.dataSource=self;
self.tbl_SlideMenu.delegate=self;
[self.view addSubview:self.tbl_SlideMenu];
}
Since added the UITableView to the UIViewController for UIView, need not to induced above two lines of code
self.tbl_SlideMenu = [[UITableView alloc]
initWithFrame:CGRectMake(0, 80, self.view.frame.size.width,self.view.frame.size.height) style:UITableViewStylePlain];
[self.view addSubview:self.tbl_SlideMenu];
Once I included the allocation and added to subview its showing the data with tableview.
Once I removed allocation and frame added to subview is not visible.
So what is need of storyboard where I added UITableView to UIViewController for UIView (view default.) once Component added to storyboard and framed need not to allocate and frame in code.
Advance Thanks
Hope for best posible answers
If u added the tableview in storyboard there is no need to initialise the view components in the code in this case u don't need below lines as u mentioned
self.tbl_SlideMenu = [[UITableView alloc]
initWithFrame:CGRectMake(0, 80, self.view.frame.size.width,self.view.frame.size.height) style:UITableViewStylePlain];
however u need to create a outlet for your tableview to access the tableview in code that means as soon as view is loaded all your view components are initialised in your case tableview is initialised and u can ready to use it and also u don't need below lines of code if u set datasource and delegate in storyboard
self.tbl_SlideMenu.dataSource=self;
self.tbl_SlideMenu.delegate=self;
since u configured your tableview datasource and delegate and also added tableview to view controller's view there is no need of below line also
[self.view addSubview:self.tbl_SlideMenu];
and second way is as u mentioned creating the view in pure code, in this case u have to do all things by your self from initialising to adding the view component to controller's view as u mentioned like below
self.tbl_SlideMenu = [[UITableView alloc]
initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain]; //creating the tableview
self.tbl_SlideMenu.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
self.tbl_SlideMenu.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; //configuring the view
self.tbl_SlideMenu.separatorColor = [UIColor lightGrayColor];
self.tbl_SlideMenu.dataSource=self; //setting the datasource and delegate
self.tbl_SlideMenu.delegate=self;
[self.view addSubview:self.tbl_SlideMenu]; //adding it to tableview
hope u get this
Edit
it is working fine for me, i crossed checked it with some dummy values, for example as i mentioned , dragged and dropped a tableview, set its datasource and delegate to View Controller in storyboard and connected a outlet in ViewController.h file, like below,
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *tbl_SlideMenu;
#end
and in ViewController.m file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tbl_SlideMenu.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.tbl_SlideMenu.separatorColor = [UIColor lightGrayColor];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CELL"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CELL"];
}
cell.textLabel.text = #"Hello";
return cell;
}
#end
and i got output showing the tableview with 5 "Hello" on each row

iOS multiple viewcontrollers

I am new to iOS programming.
I have to implement a location search based on the third party map API, but I think I have messed up multiple view controllers.
Scenario:
I have a mapviewController for displaying the mapview. And I have a searchController for searchbar. There is a resultviewController property in searchController for display the search suggestions.
I have tried to add search VC as child VC to map VC, but once I click to the search bar it returns "Application tried to present modally an active controller".
So then I remove search VC from map VC & the search bar is not activated.
Therefore I want to ask how to handle these VCs, and avoid the case that map view covers the search bar and suggestion lists.
Thank you so much.
UPDATE:
I'm building a search bar in code. My aim is to build the following style in iOS.
like this http://i1.tietuku.com/cd4efa23d97af75ft.jpg
I pushed the map viewController to the navigation controller from menu.
[self.navigationController pushViewController:mapviewController animated:YES];
And trying to add a searchController to it.
- (void)viewDidLoad {
[super viewDidLoad];
_resultsTableController = [[UITableViewController alloc] init];
_searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
_searchController.searchResultsUpdater = self;
[_searchController.searchBar sizeToFit];
_resultsTableController.tableView.tableHeaderView = _searchController.searchBar;
self.searchController.searchBar.delegate = self;
_resultsTableController.tableView.delegate = self;
_searchController.delegate = self;
_searchController.dimsBackgroundDuringPresentation = NO;
_searchController.searchBar.delegate = self;
mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, kHeight(100), kScreen_Width , kScreen_Height-kHeight(100))];
[self addChildViewController:_searchController];
[self.view addSubview: _searchController.view];
[self.view addSubview:mapView];
}
for UISearchController you no need to add it as child view controller and and instant of using UITableViewController uses UITableView it make easy for you and
here below is code that is .m file
free to run that code and without uses storyboard.
#interface ViewController ()
{
UITableView *tableview;
UISearchController *searchcontroller;
NSMutableArray *array;}
#end
#implementation ViewController
(void)viewDidLoad
{
[super viewDidLoad];
array = [[NSMutableArray alloc]initWithObjects:#"one",#"two",#"three",#"four", nil];
tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)/2)];
[self.view addSubview:tableview];
tableview.dataSource = self;
tableview.delegate =self;
searchcontroller = [[UISearchController alloc]initWithSearchResultsController:nil]; // Create Reference of searchController
searchcontroller.searchResultsUpdater = self;
searchcontroller.searchBar.backgroundColor = [UIColor redColor];
searchcontroller.searchBar.barStyle = UIBarStyleBlack;
searchcontroller.searchBar.barTintColor = [UIColor whiteColor];
searchcontroller.dimsBackgroundDuringPresentation = false;
[searchcontroller.searchBar sizeToFit];
tableview.tableHeaderView = searchcontroller.searchBar;
MKMapView *mapview = [[MKMapView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(tableview.frame), CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)/2)];
[self.view addSubview:mapview];
}
(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController;
{
NSLog(#"update ");
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
// Default is 1 if not implemented
{
return 1;
}
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return array.count;
}
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
}
cell.textLabel.text = array[indexPath.row];
return cell;
}
According to me you should use only one view controller which has a map on it. Over the map you can simply add custom search view and on typing/ taping search you can show search results using UITableview with some dropping animation keeping some max frame fixed, And finally on taping any result remove this UITableView and show location of map. If time permits I will create a sample for you, cant give assurance but will try. Mean while you can also go through Maps reference may be helpful

Unrecognized selector sent to instance when adding a UITableView as SubView

When i've created a UITableView in the code en add it as a subview in the code it is all working fine. There is green tableview visible.
Example of the code i've created for that.
- (void)getOverviewTable
{
// Adding the TableView
OverviewTableViewController *overviewTableViewController = [[OverviewTableViewController alloc] init];
UITableView *overviewTableView = [[UITableView alloc] initWithFrame:(CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))];
// Set the datasource and delegete will come here
[overviewTableView setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:overviewTableView];
}
But after i've set the DataSource and the Delegete for the TableView i will receive a warning.
The code in total for reaching it is as follows:
- (void)getOverviewTable
{
// Adding the TableView
OverviewTableViewController *overviewTableViewController = [[OverviewTableViewController alloc] init];
UITableView *overviewTableView = [[UITableView alloc] initWithFrame:(CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))];
// Set the datasource and delegete will come here
[overviewTableView setDataSource:overviewTableViewController];
[overviewTableView setDelegate:overviewTableViewController];
[overviewTableView setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:overviewTableView];
}
The most of the time it is just an EXC_BAD_ACCESS error, but sometimes it shows me the following error.
[UITransitionView numberOfRowsInSection:]: unrecognized selector sent to instance
The 'OverViewTableViewController' is replaced by just an empty UITableViewController class but the error is still showing up.
(Point of the application is an fullscreen takeover which has to be swiped away to see the tableview underneath it)
Edit:
The function is called from the viewDidLoad function
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"bg.jpg"]]];
// Setting the home takeover
[self setHomeTakeOver];
// Wait one seconde before we load the tableview
[self getOverviewTable];
//[self performSelector:#selector(getOverviewTable) withObject:nil afterDelay:1.0];
}
What I think is you have your project ARC enabled and from your code segment what i understand is that overviewTableViewController is released through ARC as soon as it leaves the scope of the function getOverviewTable. So, better declare the overviewTableViewController in .h file(#property (nonatomic, strong) OverviewTableViewController *overviewTableViewController; ).
If you do this then the code will look like this
in .h file
#property (nonatomic, strong) OverviewTableViewController *overviewTableViewController;
//// I have strong reference rather than assign
in .m file
- (void)getOverviewTable
{
// Adding the TableView
overviewTableViewController = [[OverviewTableViewController alloc] init]; //// I have made overviewTableViewController as member variable.
UITableView *overviewTableView = [[UITableView alloc] initWithFrame:(CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))];
// Set the datasource and delegete will come here
[overviewTableView setDataSource:overviewTableViewController];
[overviewTableView setDelegate:overviewTableViewController];
[overviewTableView setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:overviewTableView];
[overviewTableView reloadData];
}
Also be sure that OverviewTableViewController has implemented the datasource methods like
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Try this.
[overviewTableView setDataSource:self];
[overviewTableView setDelegate:self];
Also confirm to tableviewDelegate and datasource and implement following methods.
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=#"CellIdentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
You might have forgotten to implement this method.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
}
Try setting the table view controller's tableView property to overviewTableView. I don't know off hand if the UITableViewController must have that property set.
- (void)getOverviewTable
{
// Adding the TableView
OverviewTableViewController *overviewTableViewController = [[OverviewTableViewController alloc] init];
UITableView *overviewTableView = [[UITableView alloc] initWithFrame:(CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))];
// Set the datasource and delegete will come here
[overviewTableView setDataSource:overviewTableViewController];
[overviewTableView setDelegate:overviewTableViewController];
//THIS IS THE NEW LINE
//a UITableViewController needs to know UITableView that it's managing
[overviewTableViewController setTableView:overviewTableView];
[overviewTableView setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:overviewTableView];
}

Selections not occurring when UISearchDisplayController is being used

I am using UISeachDisplayController with a UITableView. It is set up programatically in the viewDidLoad method for the UITableViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadProjectsAndTitles];
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 600, 44)];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar
contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsTableView.allowsSelection = TRUE;
self.tableView.tableHeaderView = searchBar;
}
Everything is working absolutely fine except that when I click on a row when the UISearchBar is being used, the - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method is not being called although it is being called perfectly well when the UISearch is not being used. My question is is this normal behaviour or should the selection method work? If it should work are there any suggestions why it might not be working?
I think you forgot to set
searchDisplayController.searchResultsDelegate = self;
searchResultsDelegate is the delegate for the table view in which the search results are displayed, and didSelectRowAtIndexPath is one of the table view delegate methods.

UIButton inside UITableViewCell steals touch from UITableView

I have a problem similar to this one but answer provided there doesn't help much.
I have UITableView with some custom UITableViewCells, those cells have some nested custom UIViews and finally some UIButtons inside. The problem, as in question specified above, is that when I touch my button touch event won't populate to UITableView and it never scrolls.
Here's some code (it's just fastest way to reproduce, it's not my actual code):
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (strong, nonatomic) IBOutlet UITableView * tableView;
#end
#implementation ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
{
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,
self.view.bounds.size.width,
self.view.bounds.size.height)
style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [[UITableViewCell alloc] init];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor redColor];
button.frame = CGRectMake(0, 0, 50, 44);
[cell.contentView addSubview:button];
return cell;
}
#end
Red button in the only cell won't let table view to bounce scroll.
Can anyone help me a bit?
P.S. Don't pay attention to the stupidity of the code I've provided, I'm aware of all issues about it. I just provided it to show what the issue is all about. It's the conflict of buttonView and scrollView.
You can change the behaviour by overriding touchesShouldCancelInContentView: in the UITableView. For this to work you'll need to replace the table view with this subclass, either in loadView or in your xib file.
#interface AutoCancelTableView: UITableView
#end
#implementation AutoCancelTableView
//
// Overriding touchesShouldCanceInContentView changes the behaviour
// of button selection in a table view to act like cell selection -
// dragging while clicking a button will cancel the click and allow
// the scrolling to occur
//
- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
return YES;
}
#end
This is the standard behaviour of UIScrollView which tableviews use. The system doesn't know that you want to scroll until you move your finger, but by that time you have already "pressed" on the button and so it assumes that's what you want to do.
You can play with a couple of properties on your tableview's scrollview to change the behaviour, but you may find they negatively impact the feel of your cells and buttons because of added delays.
self.tableView.delaysContentTouches = YES;
delaysContentTouches
If the value of this property is YES, the scroll view delays handling the touch-down gesture until it can determine if scrolling is the intent...
and
self.tableView.canCancelContentTouches = YES
canCancelContentTouches
If the value of this property is YES and a view in the content has begun tracking a finger touching it, and if the user drags the finger enough to initiate a scroll, the view receives a touchesCancelled:withEvent: message and the scroll view handles the touch as a scroll.
Because all the above answers not working for me. I add a imageView on the button and make the imageView user interface YES, then add a tap gesture on the iamgeView. In the tap gesture related methods I put the button related methods in. so all is well..
may be hack. But it work well..
Below code works for me:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell = [tableView dequeueReusableCellWithIdentifier:#"cell_id"];
cell.userInteractionEnabled = YES;
if (cell == nil) {
cell = [[[CustomCell1 alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell_id"]autorelease];
}
[cell.button addTarget:self action:#selector(button_action) forControlEvents:UIControlEventTouchUpInside];}
-(void)button_action{NSLog(#"Hello!");}
This is my custom cell:
#import "CustomCell.h"
#implementation CustomCell
#synthesize button;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
button = [[UIButton alloc]init];
button =[UIColor redColor];
[self.contentView addSubview: button];
}
return self;}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame= CGRectMake(boundsX+50 ,+15, 100, 100);
button = frame;}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{ [super setSelected:selected animated:animated];
// Configure the view for the selected state
}

Resources