How to open a new view on click of Table View Cell? - ios

I am developing an iOS app which has a TableView, and I want to do sth that when a cell of the TableView is tapped, a new view opens (and then it can get back to the Table View.) I tried some things like working with segues and new view controllers, but it didn't work.

Implement the UITableViewDelegate Protocol :
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
For this Protocol the delegate method is below:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Create Object for next view controller
NextViewController *vc=[storyboard instantiateViewControllerWithIdentifier:"NextViewController"];
[self.navigationController pushViewController:vc animated:YES];
}
}

Implement the UITableViewDelegate protocol, and implement the method
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Inside that method you can create your new view.

Related

How do I delegate a UITableViewController with static cells which embed with a container inside UIViewController?

I have a UIViewController in my storyboard. Inside this UIViewController, I have a container which embed with a tableViewController. This tableViewController is a static table. How can I use this tableViewController inside UIViewController?
I have set protocols like UITableViewDelegate, UITableViewDataSource and I have no idea how I can get that tableViewController and delegate to my UIViewController.
Below are the screen.
P.S. I was trying to make a side navigation bar. Please notify me if you need to see any code.
This is my opinion: create a delegate for your tableViewController like
#protocol SideTableViewContollerDelegate <NSObject>
- (void)didClickCellOfIndexPath:(NSIndexPath *)indexPath;
#end
#interface SideTableViewController
#property (weak, nonatomic) id<SideTableViewContollerDelegate> delegate;
#end
and inside your UIViewController, you set the controller as the tableViewController's delegate
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"go table view"]) {
SideTableViewController *stvc = (SideTableViewController *)segue.destinationViewController;
stvc.delegate = self;
}
}
and inside the tableView's delegate function "didSelectRowAtIndexPath" do:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.delegate didClickCellOfIndexPath:indexPath];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
and all that left is implementing tableView controller's delegate in the uiview controller
Turns out I have to set the class in the embed tableViewController and put the [self addChildViewController: myTableViewController] in the UIViewController class. And I can implement in the UITableViewController class to control it.
You can use segue to access the tableViewController. Just implement - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender and it will be called before viewController's viewDidLoad. See this answer: Access Container View Controller from Parent iOS

In iOS 9, if using a class other than UIViewController for a page, how can I create a constraint to avoid clobbering the status and/or tab bars?

All the guidelines suggest adjusting the Top Layout Guide to avoid clobbering the status bar. But, if the page is created with a View Controller other than UIViewController (for example, if it is created with UITableViewController because the page is mainly a table view) then it has no layout guides. How can I then avoid the status bar?
I've found the UITableViewController to be more trouble than it's worth, like this guy: How do I make my iOS7 UITableViewController NOT appear under the top status bar?
Now when I implement a table view I find it easier to make the TableView a property of the UIViewController, then you set the delegate and the datasource responsibilities to the UIViewController. From there you can customize it however you want really. You can play with the Storyboard options like the Status Bar setting (Inferred, None, Black etc) but in my experience I have found it works best by just putting a UITableView inside a UIViewController.
Example Header:
#interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *myTableView;
Example Code in Controller
#synthesize myTableView;
//**** Table View Delegate and Data Source ****//
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *myCellIdentifier = #"myCell";
MyTableViewCell *cell = (MyTableViewCell *)[myTableView dequeueReusableCellWithIdentifier:myCellIdentifier];
//Customize the cell
return cell;
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return [myDataSource count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//perform some action on click
}
Another example of UITableViewController without a NavigationController and problems with the status bar: iOS 7: UITableView shows under status bar
An UITableViewController (and all other standard view controllers) inherit from UIViewController, so they have all the properties and methods of UIViewController, including top and bottom layout guides, which you can definitely use.
Since UITableViewController is inherited from UIViewController, then in Tableviewcontroller, you can override this method
- (UIStatusBarStyle)preferredStatusBarStyle {
if(<#condition>)
{
return UIStatusBarStyleLightContent;
}
else
{
return UIStatusBarStyleDefault;
}
}
If you must use UITableViewController you can embed it inside a UIViewController with an Embed Segue and do whatever you want in the UIViewController

How can I push a new view when detail button is presses

I have a tableView. My tableView cells do have a detail accessory.
My question: How can I push another view controller when the detail button is presses.
When I normally press the cell, my ViewController1 opens. But when I tap on the little i on the right of the cell, I want to push a different view controller.
How can I do this?
it is just like DidSelectRow u need to modify DidSelectRow to accessoryButtonTappedForRowWithIndexPath
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
// if u using .Xib
yourviewcontrollerName *vie=[[yourviewcontrollerName alloc]init];
[self.navigationController pushViewController: vie animated:YES];
//// if u using storyboard
[self performSegueWithIdentifier:#"yoursequeName"
sender:self];
}
you need more information use this link Pushing to a Detail View from a Table View Cell using Xcode Storyboard
Use this code it works
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
// Push new View controller here
UIViewController *objVc=[[UIViewController alloc]init];
[self.navigationController pushViewController:objVc animated:YES]
}

UISearchDisplayController doesn't fire didSelectRowAtIndexPath

I have a UIViewController with a UISearchDisplayController in it. I have it showing the search results tableview, but clicking any cells never fires didSelectRowAtIndexPath:
#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"did select row");
}
I've seen these relevant questions:
Why won't my UISearchDisplayController fire the didSelectRowAtIndexPath method?
didSelectRowAtIndexPath does not work when uitableview datasource is filtered
but I'm not making the same mistake. The view controller implements UITableViewDelegate and UISearchDisplayDelegate. The UISearchDisplayController delegate and searchResultDelegate are hooked up to the view controller in the storyboard. But just to be sure I did the following in viewDidLoad:
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.delegate = self;
I've even tried:
self.searchDisplayController.searchResultsTableView.delegate = self;
The searchResultsTableView is the only UITableView in this view controller.
The code you show in your question is didDeselect, not didSelect. So, if that's what you actually have in your code, that's the problem.

pushing data from UITableView to another UIViewController on the same screen

have a question. I have an ipad app that contains a View Controller with a UITableView and a detail view (which is basically another view controller to display details) on the same screen. I need to pass the row data from UITableView when the -didSelectRowAtIndexPath is executed to this detail view. I am not sure how to go about doing this? Any help/pointers/sample code on how to go about doing this?
Thanks for your help in advance
Define a delegate in the UITableView view controller, and implement it in the detail view controller. When didSelectRowAtIndexPath is invoked, call the delegate method, passing the row data to the detail view controller.
The delegate definition will look something like:
#class MyTopLevelViewController;
#protocol MyTopLevelViewControllerDelegate <NSObject>
-(void)showDetail:(DetailObject *)detail;
#interface MyTopLevelViewController : UIViewController
#property (nonatomic, weak) id <MyTopLevelViewControllerDelegate> delegate;
#end
and adding the delegate to the detail view controller header:
#interface MyDetailViewController : UIViewController <MyTopLevelViewControllerDelegate>
If you are willing to pass the data on another view controller in didSelectRowAtIndexPath on same screen, you can use array as you get "objectAtIndex:indexPath.row". Following code might help you:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailFoodViewController *detailFoodViewController=[[DetailFoodViewController alloc] initWithNibName:#"DetailFoodViewController" bundle:nil];
JSCategory *jSCategory=[self.categoryResult.JSCategories objectAtIndex:indexPath.row];
detailFoodViewController.jsCategory=jSCategory;
detailFoodViewController.oldStoreLocationId=self.oldStoreId;
[self.view addSubview:detailFoodViewController.view];
[detailFoodViewController release];
}

Resources