Opening new view from TableViewController - ios

I have table view that holds an image and some text. The idea is to open a new view with the specific image and the text displayed when the user clicks the instance in the table view.
I am thinking about solving this simply by having a button overlaying the cell in the table view, but it feels like there should be a simpler built in solution, like a onCellClick(Any sender) function, but I havent found it. Some help please?

Yes there is a simple solution and that is to use the UITableViewDelegate method didSelectRowAtIndexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = cell.textLabel.text;
UIImage *image = cell.imageView.image;
// here create a new view controller and send these details to your new view controller.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil]; //Main = your storyboard name
DisplayDataViewController *displayDataViewController = [storyboard instantiateViewControllerWithIdentifier:#"displayDataViewController"];
//set data in new view controller before pushing that on top of your navigation controller
displayDataViewController.imageName = image;
displayDataViewController.displayStr = cellText;
[self.navigationController pushViewController:displayDataViewController animated:NO];
}

Related

TableViewController presented modally does not scrolls to bottom

I'm currently having a CollectionViewController which by clicking on cells will display the detail view controller (TableViewController).
Code for displaying TableVC modally:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *detailVC = [storyboard instantiateViewControllerWithIdentifier:#"DetailTimetableViewController"];
[self presentViewController:detailVC animated:true completion:nil];
Things are working fine with the code above, the Table View Controller is presented modally and the scroll is behaving properly.
However, i followed this tutorial (http://blog.matthewcheok.com/design-teardown-preview-expanding-cells/), which creates a custom UIViewControllerAnimatedTransitioning, I've had everything from the tutorial to be working completely fine.
Code for display TableVC modally (with custom animation):
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *detailVC = [storyboard instantiateViewControllerWithIdentifier:#"DetailTimetableViewController"];
ExpandingCellTransition *transition = [[ExpandingCellTransition alloc]init];
detailVC.transitioningDelegate = transition;
[self presentViewController:detailVC animated:true completion:nil];
However, everything is working fine except for when the TableViewController is presented, it doesn't allows scrolling to the bottom, and is restricted at certain height.
I've tried searching for similar problems (TableViewController unable to scroll to bottom), and tried applying their solutions but none of it works, I believe the cause of problem to be from the custom transition delegate.
set tableviewProperty tableview.UITableViewAutomaticDimension = YES
Try this
// Obj-C
self.automaticallyAdjustsScrollViewInsets = NO;
// Swift
self.automaticallyAdjustsScrollViewInsets = false
hope may help you
use following code in cellForrowAtindexPath delegate method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<##"reuseIdentifier"#> forIndexPath:indexPath];
// Configure the cell...
return cell;
}
In the MainViewController viewDidLoad method, add the following code: self.automaticallyAdjustsScrollViewInsets= NO

Segue from Table View Cell xib with disclosure Indicator to storyboard

I am new in iOS programing and I have an issue. I have a customized table view cell with disclosure indicator in a separate xib file and I want to call a viewcontroller which is in storyboard when the user taps the cell.
Here is the customized table view cell :
Here are the table view (class : TableViewController) that displays the custom table cell and the view (class : ViewController) I want to call when the user taps the customized table view cell :
Does the view I want to call have to be in a separate xib file or can I let it in my storyboard? What is the best solution?
Just use didSelectRoWAtIndexPath method of table view …DO something like this..
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
NSString* viewType = #"YourVCIdentifier";//u can have ContactVC or NavC as other options depending on ur condition
UIViewController* viewController = [storyboard instantiateViewControllerWithIdentifier:viewType];
[self.navigationController pushViewController:viewController animated:YES];
}
that will do it
Use the UITableViewDelegate method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
and push the view controller manually.
Assign a storyboard ID to the view controller in the storyboard, and then do something like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
UIViewController *destVC = [storyboard instantiateViewControllerWithIdentifier:#"your_view_controller_storyboard_ID"];
[self.navigationController pushViewController:destVC animated:YES];
}
This is where you assign a storyboard ID to your view controller:
And don't forget to set your view controller as your tableView delegate :)

Table View View Controller Show Next View

Im working with table view controllers within story board.
I currently have one tableview controller embedded in navigation controller in my storyboard. This table view controller is linked to an ObjectiveC file.
Through code i can display another table view controller with the correct data using the tableView didSelectRowAtIndexPath method.
code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
WorkoutType *workoutType = (WorkoutType *)[self.fetchedResultsController objectAtIndexPath:indexPath];
WorkoutSetViewController *detailViewController = [[WorkoutSetViewController alloc] initWithWorkoutType:workoutType];
[self.navigationController pushViewController:detailViewController animated:YES];
}
But if i try to add another table view controller in storyboard, associate it with an objectiveC view controller file and connect the first TVC to it, That table view controller is not shown upon running the code.
I need to customise the second table view visually and have tried to use a segue push...as follows:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"show"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
WorkoutType *workoutType = (WorkoutType *)[self.fetchedResultsController objectAtIndexPath:indexPath];
//WorkoutSetViewController *detailViewController = [[WorkoutSetViewController alloc] initWithWorkoutType:workoutType];
WorkoutSetViewController *destViewController = segue.destinationViewController;
destViewController.workoutType = workoutType;
//[self.navigationController pushViewController:detailViewController animated:YES];
}
}
But when i run this code, the second table view controller doesn't even load although the application doesn't crash.
Whatever I am doing wrong must be simple, any help would be appreciated.
Thank You
Simple You have to navigate through below code. (No need to set storyboard when you working in same storyboard ViewController)
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexpath.row==0)//for first row click
{
WorkoutSetViewController *objWorkoutSetVC=[[WorkoutSetViewController alloc] i nitWithNibName:#"WorkoutSetViewController" bundle:nil];
[self.navigationController pushViewController:objWorkoutSetVC animated:YES];
}
else if(indexpath.row==1)//for second row click
{
//Put code in which screen you have to navigate
}
}
Do NOT Alloc init your new view controller. Use this code instead:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
WorkoutSetViewController *detailViewController = (WorkoutSetViewController *)[storyboard instantiateViewControllerWithIdentifier:#"coolID"];
[self.navigationController pushViewController:detailViewController animated:YES];
For MainStoryboard write your storyboard name and for coolID write your view controller id name which you set in your storyboard.

UITabBar disappears when selecting UITableViewCell

I have a UITableView with a cell named 'Sunset'. Upon pressing the UITableViewCell, the view switches to 'infoView' which loads some text from a .txt file. When pressing the 'back' button, the view switches back to the UITableView but there's a problem. After loading the view, the UITabBar is gone! Since it is gone, it is impossible to go back to another view.
I have tried:
self.hidesBottomBarWhenPushed = NO;
Code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cellSelected = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellTitle = cellSelected.textLabel.text;
if (cellTitle == #"Sunrise")
{ //Swap views
informationView *iv = [[informationView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:iv animated:YES];
}
}
Of course the tab bar doesn't show. You are presenting the second view modally. If you want to keep this implementation you are going to have to add a button or something on the second view that will perform
[self.navigationController dismissModalViewControllerAnimated:YES]; //Programatically dismiss the view controller
If you want to keep the tabBar and the navigation bar functionality, you should use
[self.navigationController pushViewController:iv animated:YES];
instead of
[self presentModalViewController:iv animated:YES];

Linking two TableViews through a TabBar

I've got 2 views, the AlllocationsViewController and the LoactionsViewController which are linked to each other.
That's how it looks in storyboard:
Each cell of the AlllocationsViewController links to the LocationsViewController and sends it some data to view in a tableView: AlllocationsViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
LocationsViewController *locationsViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"LocationsViewController"];
//Will send the data from the cell to the next view into 'locationlink', which is set as property in LocationsViewController.h to retrieve the data
locationsViewController.locationlink = cell.detailTextLabel.text;
[self.navigationController pushViewController:locationsViewController animated:YES];
}
Now I want to put a TabBar through those views:
After inserting data, If I leave the code below like that, the TableCell will send the user to the TableView (LocationsViewController), but without showing the TabBar.
What do I have to change in the code below so it shows me the TabBar too? Do I have to link to the TabBar instead of the locationsViewController now? Or do I have to set the TabBar in the LocationsViewController?
Any help appreciated.
Maybe this is a little late, but i think i can help you.
I prefer to use PrepareForSegue, but in your case you can use something like this...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
ClassTabBar *TabBarController = [self.storyboard instantiateViewControllerWithIdentifier:#"TabBarClientes"];
Locations *locationViewController = [TabBarController.viewControllers objectAtIndex:0]; // you can use the index you need
locationViewController.locationlink= cell.detailTextLabel.text;
[self.navigationController pushViewController:TabBarController animated:YES];
}
Tell me if I can help you in something else

Resources