opening the detail view in searchdisplaycontroller - objective-c

- (void)viewDidLoad {
[super viewDidLoad];
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 160, 44)];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
self.tableView.tableHeaderView = searchBar;
[searchDisplayController setSearchResultsDataSource: self];
[searchDisplayController setSearchResultsDelegate: self];![enter image description here][2]
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;
// inizializziamo l'oggetto Data
_objects = [[Data alloc] init];
filteredlist=[[NSMutableArray alloc]initWithArray:_objects.lista ];
}
In this method I added so as to open the detail view
[searchDisplayController setSearchResultsDataSource: self];
[searchDisplayController setSearchResultsDelegate: self];
The only problem is that it opens the view for that cell, I instead need to open the detail view associated with that name in the list initially loaded.
The problem is to open the corresponding detail view when I do a search
When I do a search I have to open the detail view when I click on the name.

I'm not 100% sure what's you problem here but make sure that in your delegate selectors you check for the tableView parameter to distinguish your search result table view from your initial table view. E.g.:
[...]
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == self.tableView) {
// ...do your tableView stuff
} else if (tableView == searchDisplayController.searchResultsTableView) {
id someSearchResultObject = [_filteredlist objectAtIndex:indexPath.row];
SomeDetailViewController *vc = [[SomeDetailViewController alloc] initWithSearchResult:someSearchResultObject];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
}
}
[...]

I will just update Tom's answer:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == self.tableView) {
// ...do your tableView stuff
} else if (tableView == searchDisplayController.searchResultsTableView) {
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"detailViewController" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
}
}

Related

Call Multiple ViewController with UIViewController

In my project I have tableView with 10 cells. And in didSelectRowAtIndexPath all cells have multiple ViewController (files) so, my didSelectRowAtIndexPath looks like
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 0) {
CallViewController *viewc = [[CallViewController alloc] initWithNibName:#"CallViewController" bundle:nil];
[self.navigationController pushViewController:viewc animated:YES];
}else if(indexPath.row == 1) {
BirthdayViewController *viewc = [[BirthdayViewController alloc] initWithNibName:#"BirthdayViewController" bundle:nil];
[self.navigationController pushViewController:viewc animated:YES];
}
so I don't want these conditions
I want my code be clean
I would suggest you to have an array that contains the Class object of your class and then create object and push like
//view did load
mutableArr = [[NSMutableArray alloc] init];
[mutableArr addObject:[CallViewController class]];
[mutableArr addObject:[BirthdayViewController class]];
....
....
Then in your
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row < [mutableArr count]) {
Class *obj = [mutableArr objectAtIndex:indexPath.row];
UIViewController *controller = [[objc alloc] initWithNibName:NSStringFromClass(obj) bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
}
}
OR if this is looking more weird then you can do like this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *controller = nil;
switch(indexPath.row) {
case 0:
controller = [[CallViewController alloc] initWithNibName:#"CallViewController" bundle:nil];
break;
case 1;
controller = [[BirthdayViewController alloc] initWithNibName:#"BirthdayViewController " bundle:nil];
break;
}
[self.navigationController pushViewController:controller animated:YES];
}
What is your underlying data model for tableview? If you could add additional attribute as ViewController Class Name you could do something like
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *row = [rowData objectAtIndex:indexPath.row];
Class viewControllerClass =
NSClassFromString([row objectForKey:#"viewControllerClassName"]);
//Default VC init looks for nib file named as VC afaik, if not, you could
//add another attribute with init selector name
UIViewController *viewController =
[[viewControllerClass alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
}
You can create array of strings corresponding to file names and use NSClassFromString function to allocate view controllers
NSArray *viewControllers = [NSArray arrayWithObjects:#"VC1", #"VC2", #"VC3", nil];
id viewController = [[NSClassFromString([viewControllers objectAtIndex:indexPath.row]) alloc] initWithNibName:[viewControllers objectAtIndex:indexPath.row] bundle:nil];
[[self navigationController] pushViewController:viewController animated:YES];
[viewController release];
You can do this:
Add all your classes to NSArray, and select the correct class by the indexPath.row.
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray * conrollersClasses =#[[CallViewController class],[BirthdayViewController class]];
UIViewController *controller = [[conrollersClasses[indexPath.row] alloc] init];
[[self navigationController] controller animated:YES];
}

Issue with self.navigationController and pushViewController

I am having trouble pushing from a tableview to a detail view. When I click the tableview cell, the cell highlights but does not push to the detail view. I am using this to transition to the detail view:
[self.navigationController pushViewController:detailViewController animated:YES];
I have read that this is a common issue, but am somehow unable to figure out a solution. My full .m file is below. If anyone has any recommendations that would be amazing. Thank you!
#import "ViewController.h"
#import "DetailViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"title";
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL *url = [NSURL URLWithString:#"http://website.com/json.php"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
news = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
[mainTableView reloadData];
}
- (int)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [news count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MainCell"];
}
cell.textLabel.text = textForMyLabel;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: #"Back" style: UIBarButtonItemStyleBordered target: nil action: nil];
[[self navigationItem] setBackBarButtonItem: newBackButton];
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
detailViewController.title = [[news objectAtIndex:indexPath.row] objectForKey:#"name"];
detailViewController.newsArticle = [news objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
}
you try:
[self presentModalViewController: detailViewController animated:YES];
You can perform push oly with a UINavigationController, the above code will work if your Controller is UINavigationController. Since your trying to integrate two projects just check whether the RootViewController is a UINavigationController.

not able to click the search bar tableview cell

I've got a tableview containing array of names. The search bar works perfectly filtering the names in the table view.
The problem is the didSelectRowAtIndexpath is not getting fired when clicking the search tableview cell. Could you help me out?
What is the thing that I'm missing? Should I include any special delegate to involve search tableview cell click.
Below is the image and code.
-(void)search
{
nameArray = [[NSMutableArray alloc] init];
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 160, 44)];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
self.tableViewFriendsList.tableHeaderView = searchBar;
}
- (void)searchDisplayController:(UISearchDisplayController *)controller
willShowSearchResultsTableView:(UITableView *)tableView
{
[tableView setRowHeight:70];
[tableView reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.tableViewFriendsList) {
NSString *friendsID =[[[self.friendsDictionary objectForKey:#"data"] objectAtIndex:indexPath.row] objectForKey:#"id"];
[[FacebookHelper sharedFacebookHelper] postOnWallWithDelegate:self andID:friendsID];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
NSLog(#"I ve come here");
NSString *friendsID =[friendsListIdArray objectAtIndex:indexPath.row];
[[FacebookHelper sharedFacebookHelper] postOnWallWithDelegate:self andID:friendsID];
}
}
You forgot to set
searchController.searchResultsDelegate = self;
I do something in one of my projects that may be of assistance:
// add gesture to detect when table view is being tapped so that keyboard may be dismissed
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(dismissKeyboard)];
gestureRecognizer.cancelsTouchesInView = NO;
[self.tableView addGestureRecognizer:gestureRecognizer];
Moreover I am wondering why you have a search bar within a table cell. Would you mind posting a screen shot of it in your app? I am afraid you may be doing more work than is necessary.

Unable to trigger UITableView Delegate Method "DidSelectRowAtIndexPath"

Hi I am using a searchDisplayController to filter through my search results. My code snippet is as follows:
- (void)viewDidLoad
{
[super viewDidLoad];
//Set up search bar
UISearchBar *tempBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,0,320,40)];
self.sBar = tempBar;
[tempBar release];
self.sBar.delegate = self;
self.sBar.tintColor = [UIColor colorWithHexString:#"#b6c0c7"];
self.sBar.placeholder = #"Search Questions and Topics";
[self.view addSubview:sBar];
self.filteredListContent = [NSMutableArray arrayWithCapacity:[self.listContent count]];
self.searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:sBar contentsController:self];
[self setSearchDisplayController:searchDisplayController];
[searchDisplayController setDelegate:self];
[searchDisplayController setSearchResultsDataSource:self];
// restore search settings if they were saved in didReceiveMemoryWarning.
if (self.savedSearchTerm)
{
[self.searchDisplayController setActive:self.searchWasActive];
[self.searchDisplayController.searchBar setText:savedSearchTerm];
self.savedSearchTerm = nil;
}
[self.resultTableView reloadData];
self.resultTableView.scrollEnabled = YES;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"zzz");
}
Do I need to setup anything in my view did load to trigger the uitable delegate method?
Note that I have already declared UISearchDisplayDelegate, UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate in my .h files. Also my tableView cellForRowAtIndexPath method is working.
Have you actually set
self.tableView.delegate = self
or connected it up in Interface Builder

"Unlock to delete" on edit Subview

I have this table on linked to a database with a "Edit" on top-right.
I can delete swiping, but if I press the Edit button nothing happen!
I mean, I wrote a similar call for another table in the same program, and when i press edit, a small "Unlock to delete" appear; then if you press it you can delete the element/row...
not here!
- (void)viewDidLoad
{
self.title = #"Categorie";
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
[self.navigationItem setHidesBackButton:YES animated:YES];
[tableView reloadData];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)_tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// A lot of code...
}
}
You need to tell the tableView to begin editing with setEditing:(BOOL)editing animated:(BOOL)animate
//Init your editBarButton
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editTable)];
//Init your doneBarButton
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneEditing)];
Here's the a sample action method:
- (void)editTable {
if (!self.tableView.editing) [self.tableView setEditing:YES animated:YES];
self.navigationController.rightBarButtonItem = self.doneButton;
}
- (void)doneEditing {
if (self.tableView.editing) [self.tableView setEditing:NO animated:YES];
self.navigationController.rightBarButtonItem = self.editButton;
}