problem with uitableview when return - iphone

i have viewcontroller with table view that when i click on a cell i go in the navigation to another view controller with another tableview.
i try to use the viewDidAppear and viewWillAppear in the first viewcontroller, that when i back to this viewcontroller from the second viewcontroller i will enter one of this method.
the problem is that he didn't enter this method when i return to this viewcontroller.
this is how i enter the second view controller:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
ProfileViewController2 *tmp = [[ProfileViewController2 alloc]initWithType:indexPath.row string:[self.array2 objectAtIndex:indexPath.row]];
[[self navigationController] pushViewController:tmp animated:YES];
[tmp release];
[self.mytableview deselectRowAtIndexPath:indexPath animated:YES];
}

viewWillAppear and viewDidAppear are notoriously shaky on iOS.
If you are working with UITableViews we always put the code we need to run before the table gets loaded into the
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
function. It's a slight hack but works very well as this is the first function out of the UITableViewDataSource protocol called.
Alternatively, you can call
[tmp viewDidAppear];
After pushing the view controller and before releasing tmp to force the function being called.

Related

navigation from tableview to another view

I am working in a table view app using story board . I want to perform event on selection
of cell in table view . So when I select a cell element , a next view(detailview) should be
open for displaying further details .
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.dv = [[Detail alloc]initWithNibName:#"Detail" bundle:nil];
dv.dic = [self.arr1 objectAtIndex:indexPath.row];
[self.view addSubview:dv.view];
}
Instead try this:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.dv = [[Detail alloc]initWithNibName:#"Detail" bundle:nil];
dv.dic = [self.arr1 objectAtIndex:indexPath.row];
[self presentViewController:dv animated:YES completion:nil];
}
And if you want to dismiss use this code:
[self dismissViewControllerAnimated:YES completion:nil];
You should use a navigation controller and when selecting a row, push the detail view into it.
To create navigation controller: selected the tableview controller and go Editor / Embed in / Navigation Controller.
Then do:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// dv = [Detail alloc] initWithNibName:#"Detail" bundle:nil];
dv = [storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
dv.dic = [self.arr1 objectAtIndex:indexPath.row];
[self.navigationController pushViewController:dv];
}
The commented line is if you're not using storyboard.
Or you can do as Abdullah suggested, but implement a dismiss button with
[self dismissViewControllerAnimated:YES completion:nil];
In the storyboard drag a segue from the viewcontroller icon at the bottom of your view to the next viewcontroller you want to segue to. Make sure that your view is embedded in a navigation controller. Press the arrow that appears between your tableviewcontroller and the target view controller and make sure you label it with a segue identifier. Then in the method you are describing make sure they selected the correct index path with an if statement and write
[self performSegueWithIdentifier:#"theNameOfYourIdentifierFromTheStoryboard"];
That being said, I think that there is a large body of information you should research.
Read this: http://www.appcoda.com/use-storyboards-to-build-navigation-controller-and-table-view/ as a start.

UITableView in a UIViewController linked to another ViewController (DetailController)

I have an app on the store that uses a RootViewController linking to a UIViewController (DetailController) and I am working on a new app which basically requires the need for this same feature. But instead, my new app has a UITableView inside a UIViewController linked to a UIViewController. So I thought, i'd copy and paste my RootViewController code into this new UIViewController. So i've linked up the TableView, set delegate and datasource to self and the TableView shows the titles of the items (Hurrah) but when touched, doesn't go to the DetailController? I've used NSLog to determine what part isn't working and of course its the didSelectRowAtIndexPath method… and here is my code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *theItem = [items objectAtIndex:indexPath.row];
DetailController *nextController = [[DetailController alloc] initWithItem:theItem];
[self.navigationController pushViewController:nextController animated:YES];
[nextController release];
}
The TableViewCell just highlights blue and doesn't link to DetailController.
Thanks in advance!
By "not working" do mean it's not being called, or that it's not pushing the view controller? If the first, then make sure the table view delegate is set correctly. If the second, make sure both nextController and self.navigationController are not nil.

Add tableView to navigationController

In viewDidLoad I add a tableview to navigationController like this :
[self.view addSubview:navigationController.view];
[self.navigationController.view addSubview:tableViewRecherchePartenaire];
When a row from tableview is selected I want this tableview to disappear and I tried something like this :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==0){
NSLog(#"RecherchePartenaireTypePartenaireViewController...");
[self.navigationController.view removeFromSuperview];
recherchePartenaireTypePartenaireViewController=[[RecherchePartenaireTypePartenaireViewController alloc] init];
recherchePartenaireTypePartenaireViewController.recherchePartenaireViewController=self.view;
[self.navigationController pushViewController:recherchePartenaireTypePartenaireViewController animated:YES];
[recherchePartenaireTypePartenaireViewController release];
}
}
but the tableview disappear but the next view is not appear. How can I use the table view with the navigationController ? Please help...I spent a lot of time with this and I can see the mistake..Thanks in advance..
First of all, you must not add subview to UINavigationController's view. Put your UITableView in a subclass of UIViewController and make it the rootViewController of your navigationController. You can do it in your main xib file or programmatically :
navigationController.rootViewController = myTableViewController;
After you can almost keep your function :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row==0){
NSLog(#"RecherchePartenaireTypePartenaireViewController...");
recherchePartenaireTypePartenaireViewController=[[RecherchePartenaireTypePartenaireViewController alloc] init];
[self.navigationController pushViewController:recherchePartenaireTypePartenaireViewController animated:YES];
[recherchePartenaireTypePartenaireViewController release];
}
}
But this function will be in myTableViewController.
In your code, you were removing your navigationController's view from the view hierarchy. So it's normal that nothing came up when you were pushing something, because UINavigationController handle push by pushing the next controller's view on it's own view.
Here is a nice Apple's sample that could help you :)
http://developer.apple.com/library/ios/#samplecode/TheElements/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007419
In your code uitableview is not a navigation type view.So,When you are adding the UITableview to the navigationcontroller's view,superview is uitableview.Ans in didselectrowAtIndexPath you are pushing on the UITableview but not on the NavigationController.So make the UITableview as navigationtype.And then add it to navigationController.And do the same whatever you did.

Deselect row in UITableView which is a subview

I have a UITableView which is added to a view as a subview. When selecting a row a new view will be present with pushViewController: and the user has the option to push a Back-button to go back to the UITableView but then the cell is still selected.
Is there a way to deselect this when the view appears?
I have read that I should use the following code but I can't get it working. No errors, no warnings, no nothing.
[tableProgram deselectRowAtIndexPath:[tableProgram indexPathForSelectedRow] animated:NO];
You should deselect the row in the didSelectRowAtIndextPath method of the delegate of your UITableView. It should look something like this.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
/* initialize your view controller here v and then push it */
SomeViewController *v = [[[SomeViewController alloc] init] autorelease];
[self.navigationController pushViewController:v animated:YES];
}
Dot confuse it with didDeselectRowAtIndextPath method - it happens as people do not pay much attention when selecting methods from intelisense.
Another place you can use this is inside viewDidAppear
- (void)viewDidAppear:(BOOL)animated
and insert the following
NSIndexPath *indexPath = [tableView indexPathForSelectedRow];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
The cell stays selected after clicked and pushed to a different view controller, but deselects when it pops back to the original view controller, so it gives the user a visual cue for the last selected row.
quote SimonBS:
tried that and hoped it would work (to
get the animation) but it seems that
both viewDidAppear:(BOOL)animated and
viewWillAppear:(BOOL)animated aren't
called when in a subview.
the code by honcheng does work
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSIndexPath *indexPath = [self.tableViewOutlet indexPathForSelectedRow];
[self.tableViewOutlet deselectRowAtIndexPath:indexPath animated:YES];
}
you just need to use it on the TableView's superview. (For instance: if you have a xib file with a TableView subview inside the view you just need to go to the corresponding view controller code and override viewWillAppear, i just did it and it works! hello visual cue!)

Code is exectuting but the view is not loading when called form a function?

I am doing a book kinda application using a viewBased application. The mainView acts as the backgroundView for loading pages. Each page is a different view and have its own nib. When i swipe on a page(actually on the mainView which have loaded the current view/page), that page will be removed and the next/previous page will be added.
[currentPage.view removeFromSuperview];
[self.view insertSubview:nextPage.view atIndex:0];
I have added a popoverController with a barbutton in this mainView. Its intialized with a tableviewController class named popClass. PopClass is another UITableViewController class which will act as the parent for the popViewController.
self.myPopController = [[PopController alloc] initWithStyle:UITableViewStylePlain];
self.myPopOverController = [[UIPopoverController alloc] initWithContentViewController:myPopController];
When I select certain row in the popover, I want the backgroundview(mainView) to load the page corresponding to the number of the row.
For that I have written a function in bookView(mainView) Controller class. And is calling it from the popOver's parent class when i select certain row.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger toPageNumber = indexPath.row + 1;
[viewController changeToPage:toPageNumber];
}
Function and the lines of code are executing but nothing is happening. Even the log lines are printed but no action takes place. The code is the same for removing a page and adding another page which is working successfully when i swipe the view/page.
What is the problem? OR is there anyother way to make the view change in mainView by using the popover?
You need to have refernce of the background view controller in the popover controller.
In the PopController controller you should have a delagate like:
#interface PopController: UITablViewController{
//// other varibales;
id delegate;
}
#property (nonatomic, retain) id delgate;
Now from the class in which you are showing the popovercontroller
you should add this line:
[myPopController setDelegate:self];
Then you can easily acces any method of the main view controller class. By using
[delegate callTheRequiredMethod];
Hope this helps.
Thanks,
Madhup
It's hard to tell what's going wrong without looking at the code. That said, here's what I'd do:
(1) When a row is selected in mainView, present the popoverController modally:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
PopoverViewController *vc = [[PopoverViewController alloc] init];
[self.navigationController presentModalViewController:vc animated:YES];
[vc release];
}
(2) When something changes in the popoverController, for example a row is selected, set a value in the parentViewController (which should be the MainView):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.parentViewController.value = someValue;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
(3) Dismiss the popoverController where-ever you choose by calling:
[self dismissModalViewControllerAnimated:YES]`