May I add a table view in a view controller? - ios

I have 2 controllers.
I want to add a table view in the view controller,
and the table view's cell has the custom class of tableviewcell.
I tried to use [self addsubview:(the controller.view) into the controller.
I can see the list, but cannot set each cell's value.
In my viewcontroller:
TableViewController *viewController = [[TableViewController alloc] init];
[self addChildViewController:viewController];
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];
I try to init the tableviewcontroller in my viewcontroller. Now i can see the tableview. But i cannot set the value of each cell. even more the break point cannot stop in the
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

You will also need to provide a delegate and a data source for your table view, and implement the required methods. I have a strong suspicion you skipped a couple crucial steps here. cellForRowAtIndexPath will never get called unless your view controller is properly set up as the UITableView's delegate and data source.
See the UITableView programming guide for more info.

Related

Dismiss navigation controller and all of its view controllers stack in UIPopupview using a button within one of the views

I am displaying a UIPopoverView using the code:
selectClientPopController= [[UIPopoverController alloc]initWithContentViewController: [self.storyboard instantiateViewControllerWithIdentifier:#"navControllerMini"]];
selectClientPopController.popoverContentSize=CGSizeMake(500, 700);
selectClientPopController.delegate=self;
[selectClientPopController presentPopoverFromRect:CGRectMake(cell.frame.origin.x+120-scrollView.contentOffset.x, cell.frame.origin.y+120,cell.frame.size.width, cell.frame.size.height) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionRight animated:YES];
This navigation view controller is ued to step through a two TableViewControllers as shown below:
When a cell on the last tableView is pressed, I would like to dismiss the whole popover.
I understand that this probably needs to be done using delegates, however I do not know where to assign the delegates in order to achieve this. Thanks for the help.
Why Not
// Listen for TableView row selection by setting up delegate.
// Basically self if you set the delegate in the same view controller
YourTableView.delegate = YourViewControllerObjectWhichHasTableView;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[selectClientPopController dismissPopoverAnimated:YES];
}
Whenever u need to dismiss the popover just use the above method. Refer the Apple's Documentation

cannot transition between view controller when clicking a table cell

I want to transition from one view controller into another when clicking a table cell without any additional data passing. Here is a screenshot of my storyboard: http://imgur.com/C8yTRaZ
However, it doesn't go to the second view controller when I am clicking on the table cell. They are both embedded in the navigation controller (I think; since the "Navigation Item" is in both controllers). What am I missing?
Make sure you have mapped your table data source and table delegate to self.
For that you could use didSelectRowAtIndexPath in that you could programmatically call that other view Controller.
Like below:-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewController *viewController = [[ViewController alloc] init];
[self presentViewController:viewController animated:YES completion:nil];
}
This will help you out.
Make sure that 'user interaction' for both 'table view' and 'table view cell' should be enabled.

DetailViewController Not Updating For Animation

I have a UISplitview controller (with a master and detail view). So, when the detailview loads the viewDidLoad calls a function named [self animateToPosition]; with the following code:
-(void*)animateToPosition
{
NSLog(#"Called Function");
[worldV animateToPosition:MaplyCoordinateMakeWithDegrees(-122.4192, 37.7793) time:1.0];
[self.view setNeedsDisplay];
return 0;
}
Now, when this is first run, my globeViewC correctly animates to the position passed in (worldV is initialized and created an all in viewDidLoad of detailviewController also "Called Function" is printed in the log.
However, I have a view controller and in the didSelectRowAtIndexPath I have the following code (in the file masterViewController.m)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[testArray objectAtIndex:indexPath.row]isEqualToString:#"Menu1"]) {
DetailViewController *test=[[DetailViewController alloc]init];
[self addChildViewController:test];
[self.view addSubview:test.view];
[test didMoveToParentViewController:self];
[test animateToPosition];
}
}
When I select the cell called menu1, "Called function" DOES display in the NSLog, but the animation that occurred when the view was first loaded doesn't anymore. I'm wondering if I must somehow reload the detailviewcontroller or initialize it again (but I tried initializing it again and that didn't work)
Would like if someone could see this and help me get this working.
It would be helpful if you could show more, especially how you alloc and init your worldV and what you have in your viewDidLoad.
Normally you don't create a new DetailViewController for SplitView setup.
Instead of creating a new DetailViewController, try to grab a hold of the existing detail view controller by creating a property:
#property(strong, nonatomic) DetailViewController *detailViewController;
and:
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
First thing to test here is whether your worldV is nil. A simple NSLog in animateToPosition would reveal the answer.
And whenever you need to call that method, do:
[self.detailViewController animateToPosition];
To animate from one coordinate to another with time, you may need to change your animateToPosition method to:
-(void)animateToPosition:(MaplyCoordinate)coordinates time:(CGFloat)time
Consult whether or not your MaplyCoordinate is an object, which I doubt.
So, to sum up, in your tableview's viewDidLoad, get a hold of the detail view controller.
When didSelectRow gets called, use the new method suggested and pass the coordinate and time this way:
[self.detailViewController animateToPosition:coordinates time:1.0];
Hope this helps.
In didSelectRowAtIndexPath function you create an DetailViewController but you don't add it to the view hierarchy. You should add it to the parent view controller like this
[self addChildViewController:viewController];
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];
or just add it to navigation or present it as a modalViewController

Calling modalTransitionStyle from UITableViewController - nothing happens

I finally managed to trim my code and put all the TableViewController-Methods in a separate class (away from my ViewController). I had many problems which I already solved but there is one that puzzles me.
When a selection in the table occurs, I previously loaded a modalViewController, which worked fine. But now, as I put all the Table-methods in a separate class, the modal view simply does not load. It calls the function, and steps through it nicely when I select a row in the table, but there is no view loaded on the screen of my iPhone?!
Here is the code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
TaskViewController *taskViewController = [[TaskViewController alloc] initWithNibName:#"TaskViewController"
bundle:nil
task:[listOfEntries objectAtIndex:indexPath.row]];
taskViewController.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:taskViewController animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
any suggestions?
Problems loading the NIB file? Not returning self from TaskViewController's init method? Are you sure taskViewController is not nil? Maybe you should call this on a parent navigationController? Are you setting UIModalPresentationFullScreen as the modal presentation style?

Simple Detail View Help?

Im having major problems with Detail views and was hoping someone could point me in the right direction.
I would like to add a detail view to an app like apples advancetableviewcells. So once you click on a cell, it leads you to the detail view.
Only catch is, it has to load the data from the cell into the detail view also.
I've looked at allot of sites and allot of examples online, and I can see how it done with simple table but when it comes to individual cells and the data in the .plist. I'm lost.
Please any help would be great.
Thanks
Adding a detail controller isn't that difficult to implement. In your root view - I guess - you collect all the data from your plist file you mentioned above and order the data after your own criterias. Why don't you create a NSArray in the root view controller which contains a NSDictionary for every cell. In that dictionary you put all the information like title, price or something else of that object. In your detail controller just add an NSDictionary property.
If you select a cell, the didSelectRowAtIndexPath method is called. In this method you set the NSDictionary property in the detail view controller as the object in the NSArray of your root view controller at the index indexPath.row
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *myDetailController = [[DetailViewController alloc] initWithNibName:#"NibBame" bundle:nil];
myDetailController.detailDict = [rootViewArray objectAtIndex:indexPath.row];
[self.navigationController pushViewController:myDetailController animated:YES];
[myDetailController release];
}
Afterwards all the data for the specific cell is now available in the detail view controller, and you can use the advantages of the NSDictionary and can get the variables just by a simple key. For example:
- (void)viewDidLoad {
self.title = [myDictionary valueForKey:#"specificKey"];
}
The table cell needs to be subclassed from UITableViewCell. You don't have to set the title of the view controller three or more times, it is always overwritten. You should set up your custom cell in the cellForRowAtIndex method and set the properties of the cell.

Resources