Custom UISearchBar in navigationItem ios7 - iphone

I have a customized UISearchBar that I want to display in self.navigationItem.titleView. I have no problem getting it in there and looking the way I want it to but I think something is wrong with how I connect it with UISearchDisplayController.
Here is the UISearchBar and UISearchDisplayController in viewDidLoad:
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0,0.0,200.0, 22.0)];
searchBar.barStyle = UISearchBarStyleDefault;
searchBar.showsCancelButton = NO;
searchBar.placeholder = #"Search";
searchBar.delegate = self;
self.navigationItem.titleView = searchBar;
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDelegate = self;
searchDisplayController.searchResultsDataSource = self;
At this point the search function is not actually working. If I add [searchDisplayController setDisplaysSearchBarInNavigationBar:YES]; the search function now works but it messes up all the style I that I had added to searchBar.
Edit: Additional information: looks like [self.searchDisplayController.searchResultsTableView reloadData]; is not getting called. I have the following implemented:
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
For some reason if I don't call [searchDisplayController setDisplaysSearchBarInNavigationBar:YES]; then the data is not reloaded. Perhaps I am missing a method.
Upon searching it looks like - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section gets called and tableView == self.searchDisplayController.searchResultsTableView is true, but it does not proceed to call - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

Well, I think the only way to get it to work is to embed UISearchBar into a UIView, I'm okay with the style now although I have been able to make it exactly the same.

Related

In ios7 tableView:didSelectRowAtIndexPath: is not called

I meagrate my project to adjust ios7 while I encountered a strange problem : the delegate method "tableView:didSelectRowAtIndexPath:" is not called in ios7 ,it works well in prior ios version.I was wonder if some specific property be changed in ios7
Here is the code:
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView setDelegate:self];
[self.tableView setDataSource:self];
[self.tableView setAllowsMultipleSelection:NO];
[self.tableView setMultipleTouchEnabled:NO];
}
- (void)tableView:(UITableView *)sender didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[sender deselectRowAtIndexPath:indexPath animated:YES];
_currentContact = [contactArr objectAtIndex:indexPath.row];
if ([_currentContact.accountNPC hasPrefix:#"0"]) {
isContactToNPC = NO;
}else{
isContactToNPC = YES;
}
....
}
Add the following in your viewController.h file
<UITableViewDelegate,UITableViewDatasource>
also connect the tableview's delegate and datasource to the File's Owner of .xib
I'm sorry,I found the answer
I just need set
[cell setExclusiveTouch:YES]
Tks #abhishekkharwar from https://stackoverflow.com/a/18826264/2396477

CellForRowAtIndexPath is not invoked

I'm writing an app hat has many views and I used sliding views library (ECSlidingViews). The problem is when I fill an array with objects and fill the table with the objects in tableView:cellForRowIndexPath: method, it does present the data in the table, but when I go to other view and come back the data disappears because tableView:cellForRowIndexPath: is not called. Here is the code:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"begin of cellForRowAtIndexPath");
SchedualeCell *cell = (SchedualeCell *)[tableView dequeueReusableCellWithIdentifier:#"Cell"];
Course *temp = [array objectAtIndex:indexPath.row];
cell.nameLb.text = temp.name;
cell.hourLb.text = [NSString stringWithFormat:#"%d",temp.hour];
cell.startTimeLb.text = temp.startTime;
cell.endTimeLb.text = temp.endTime;
NSLog(#"End of cellForRowAtIndexPath");
return cell;
}
tableView:numberOfRowsInSection: and numberOfSectionsInTableView: is invoked when I come back to the view that has the table view.
P.S.: The view is UIViewController that has table view inside of it, and I searched all StackOverflow before posting my problem.
EDIT : this is where I set the delegate and datasource
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"Did Appear");
// Do any additional setup after loading the view.
self.view.layer.shadowOpacity = 0.75f;
self.view.layer.shadowRadius = 10.0f;
self.view.layer.shadowColor= [UIColor blackColor].CGColor;
if (![self.slidingViewController.underLeftViewController isKindOfClass:[MenuViewController class]])
{
self.slidingViewController.underLeftViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"Menu"];
}
[self.view addGestureRecognizer:self.slidingViewController.panGesture];
if (array == nil)
{
array = [[NSMutableArray alloc]init];
}
table.delegate = self;
table.dataSource = self;
[table reloadData];
}
and I did included The delegate and datasource in the header
#interface MainViewController : UIViewController<UITableViewDataSource , UITableViewDelegate,addDelegate>
First of all , it's not numberOfRowsAtSection and numberOfTableView. It's numberOfSectionsInTableView and numberOfRowsInSection.
Things you can do :
1) NSLog your numberOfRowsInSection. Note that , If it's "0" then your cellForRowAtIndexPath is never going to be called.
2) If you are using your UITableView inside some UIView then you should add :
[self.view addSubview:table];
3) Don't Forget to include :
#interface yourViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
Once check that you reframed the tableview in ViewWillAppear.
Some times if we set out of frame, CellForRowAtIndexPath will not call.

Three20 TTTableViewController and searchViewController

I am trying to implement adding recipients to TTMessageController based on the example from TTCatalog.
Everything works fine until I enter search controller - I can search and get results, but nothing happens when I select items. I tried to set delegates, but none works.
- (void)loadView {
[super loadView];
TTTableViewController* searchController = [[TTTableViewController alloc] init];
searchController.dataSource = [[FriendsDataSource alloc] initWithApi:self.appDelegate.api];
searchController.variableHeightRows=YES;
self.searchViewController = searchController;
self.tableView.tableHeaderView = _searchController.searchBar;
}
In TTCatalog, items in search have an URL directing to google - that's not very helpful ;)
OK, I've found the answer :)
One line was missing from the code above. I had a feeling I should set the delegate, but didn't know where:
- (void)loadView {
[super loadView];
TTTableViewController* searchController = [[TTTableViewController alloc] init];
searchController.dataSource = [[FriendsDataSource alloc] initWithApi:self.appDelegate.api];
self.searchViewController = searchController;
_searchController.searchResultsTableView.delegate=self;
self.tableView.tableHeaderView = _searchController.searchBar;
}
Then it as enough to add this to allow variable heights:
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
id<TTTableViewDataSource> dataSource = (id<TTTableViewDataSource>)tableView.dataSource;
id object = [dataSource tableView:tableView objectForRowAtIndexPath:indexPath];
Class cls = [dataSource tableView:tableView cellClassForObject:object];
return [cls tableView:tableView rowHeightForObject:object];
}
And this to handle selection:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
TTTableImageItemCell *cell = (TTTableImageItemCell *) [tableView cellForRowAtIndexPath:indexPath];
TTTableImageItem *object = [cell object];
[_delegate MessageAddRecipient:self didSelectObject:object];
}

addSubview insertSubview aboveSubview bit confused as to why it does not work

I'm a bit confused all as to why the belowSubview does not work.
I'm adding some (subviews) to my navigationController and all of that works well.
-(UITableView *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
...
[self.navigationController.view addSubview:ImageView];
[self.navigationController.view addSubview:toolbar];
add some point in my app I wish to add another toolbar or image above my toolbar.
so let's say I'm doing something like this
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...
...
[self.navigationController.view insertSubview:NewImageView aboveSubview:toolbar];
//crucial of course [edit]
rvController = [RootViewController alloc] initWithNibName:#"RootViewController" bundle:[NSBundle] mainBundle];
rvController.CurrentLevel += 1;
rvController.CurrentTitle = [dictionary objectForKey:#"Title"];
[self.navigationController pushViewController:rvController animated:YES];
rvController.tableDataSource = Children;
[rvController release];
However this doesn't work..
Does anyone know what I'm doing wrong here ...
Should I have used something else instead of addSubview or is the problem somewhere else?
From what you have posted, it looks like that should work.
There are a few other issues however. First it is convention that varibales that represent instances of objects start with lowercase. So ImageView and NewImageView should be imageView and newImageView.
I would make sure that in your tableView:didSelectRowAtIndexPath: method newImageView and toolbar are both valid. Are they in your header file?
Try this and see where is errors out:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...
...
NSAssert(self.navigationController.view,#"WTF? self.navigationController.view is nil");
NSAssert([self.navigationController.view superview],#"WTF? lf.navigationController.view is not onscreen");
NSAssert(newImageView,#"WTF? newImageView is nil");
NSAssert(toolbar,#"WTF? toolbar is nil");
NSAssert([toolbar superview],#"WTF? toolbar is on in the view");
[self.navigationController.view insertSubview:newImageView aboveSubview:toolbar];
}

Call UIViewController from "subview"

I have a UIViewController which when it loads it loads up this..
MapViewController *mapController = [[MapViewController alloc] initWithNibName:#"MapView" bundle:nil];
self.mapViewController = mapController;
[self.view insertSubview:mapController.view atIndex:0];
[mapController release];
I also have a switch views button that can change to a table view....
if (self.tableViewController ==nil)
{
TableViewController *tableController = [[TableViewController alloc] initWithNibName:#"TableView" bundle:nil];
self.tableViewController = tableController;
[tableController release];
//[self.view insertSubview:detailController atIndex:0];
}
if (self.mapViewController.view.superview == nil)
{
[tableViewController.view removeFromSuperview];
[self.view insertSubview:mapViewController.view atIndex:0];
}
else
{
[mapViewController.view removeFromSuperview];
[self.view insertSubview:tableViewController.view atIndex:0];
}
I am trying to change the view to a detail view based on selecting a row in the table view and I cannot work out how to call it at all. All methods I have seem to fail! Please help!
Add the UITableViewDelegate protocol to your controller class.
#interface myTableViewController : UITableViewController <UITableViewDelegate>
When you create your table veiw controller set its delegate to be your controller using:
myTableViewController.delegate = self; // Assuming your setup code runs within the table view controller
In your tableViewController, implement didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int rowSelected = [indexPath indexAtPosition:0]; // Assuming your UITableView has a single section.
The class you should look at for handling row selection is UITableViewDelegate, which has the method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath