How to setup a static UITableView as a subview of UIView? - ios

When I work with a TableViewController I am able to setup all my content in storyboards. Since I use Static Cells instead of Dynamic Properties for my table view, I find this method much more convenient and easier to implement. I hook up the new UITableView class and simply delete all the delegate methods. Works like a charm as ALL of the content / buttons are being setup in storyboards.
I am trying to accomplish the same result, except this time, I have to work within a ViewController and add a TableView as a subview. Once I hook up the right class, add my outlet connection and setup the following delegates:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MainCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
This works well if my TableView is set to Dynamic Properties :
But when I change the Table View content to Static Cells and delete the delegate method, my app crashes. So, How can I add a table view with Static Cells (That I can manipulate in storyboards) to my ViewController?

Here is what you can do. In your storyboard, create a parent view controller that contains all your non tableview views also, create a UITableViewController. In your parent view controller, create container view, delete the view controller it auto adds, and right click and drag from the container view to your UITableViewController to create an embed segue. Your end result should look something like this:

As far as I know, you can't do this directly. At least in iOS 6, you had to use a UITableViewController when using static cells. One way to use a static table view inside a UIViewController would be to add a container view in IB, and make the embedded controller a table view controller (delete the UIViewController you get automatically, drag in a UITableViewController, and hook it up with the embed segue). You can get a reference to this table view controller from the UIViewController by implementing prepareForSegue:sender:, and using the destinationViewController property (which will point to the table view controller).

You still need to do a couple of things:
Add <UITableViewDataSource, UITableViewDelegate> to your #interface declaration.
Then you can set these also in Interface Builder.
Implement cellForRowAtIndexPath and call the dequeueReusableCellWithIdentifier method to return the cell.
Sorry, I was wrong. The truth is you cannot use static cells without a UITableViewController. Sorry.
A solution could be that you create two controllers and just add the view of the table view controller to your other view controller.

Related

TableView cell is not pushing to next view controller

I created the single view application using storyboard.
In storyboard file, i have three view controller
1-navigation controller 2-Recipe Book View Controller 3-View
Controller
Prototype cell of the table view of Recipe Book View Controller is connected through push segue to View Controller.Is the problem Recipe Book View Controller does not navigate to View Controller?
here is the sample of project for download.
https://drive.google.com/open?id=0B5pNDpbvZ8SnLTd1R3NBTE1ReEk
I just Check your project.
Do following steps:-
In Main.storyboard, click on Push segue to ViewController and add name of identifier
Go to ViewController.m and paste below code
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:#"yourSegueName" sender:self];
}
Following mistakes are found in your project.
There is no class for Third VC.
PrepareForSegue not implemented.
You wanted to push VC on TableViewCell selection but there you does not implemented didSelect tableview delegate.
Select the segue connecting RecipeBookViewController to ViewController , Then give that segue an identifier, i.e. "ViewControllerSegue".
Implement the delegate method of table view and call the performSegueWithIdentifier method :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:#"ViewControllerSegue" sender:tableView];
}
Based on your Source code you have to add some code & classes in your project to achieve your requirement.
1st You have to add RecipeViewController to control your Recipe List and Tableview both.
2nd as You have created this RecipeViewController you need to assign this class to respective ViewController in your storyboard.
3rd Assign Segue from PrototypeCell to ViewController where you need to push.
Hope you can understand whats wrong in your Code.

iOS: Implement UITableView using MVC

I couldn't find an example with good explanation how to include UITableView in my project using MVC pattern.
Let's say that at the beginning I have only two files 'MainViewController' (:UIViewController), and 'MainModel' (:NSObject) containing my Array with data for cells.
Where I should have a reference to UITableView object, which file should be delegate for table, ... ?
Your MainViewController will have a view property that you should point to your UITableView instance. You can have any object be your delegate, but usually your delegate is the view controller that controls it, which would be your MainViewController.
That said, there's a subclass of UIViewController called UITableViewController that you should probably be using as the superclass of MainViewController. It has some automatic functionality for controlling UITableViews. In fact, instantiating a UITableViewController (or any its subclasses) will automatically create a UITableView and point to it in its view & tableView properties.
So your MainModel is the model. MainViewController is your controller. The tableView is your view. Add it as a property to your MainViewController and set your MainViewController to be the dataSource and delegate for the tableView. Then when you are populating the tableView, use the data in your MainModel. For instance:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[MainModel sharedInstance] myArray].count;
}
Or create an NSArray property on MainViewController and populate it from MainModel in viewDidLoad or viewWillAppear.

Table view cell to another view controller

I'm creating a small app with StoryBoard, that currently consist of a Navigation Controller, Table View Controller and a regular view controller. I have an array of objects that is connected to the table view, and now I want to select one of the objects and get the regular view controller on the screen. So, in StoryBoard I CTRL-drag from the prototype to the view controller in order to create a segue (push). Now, I want it to work, but literally nothing happens.
What am I doing wrong? Shouldn't this work directly or am I missing to set some options? I really want this to work with the StoryBoard but I tested to give the segue an identifier and implement the below metod, but that didn't work either.
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:#"showInfo" sender:self];
}
for navigating from UITableViewCell, You need to write your "code for navigation" in UITABLEVIEWDELEGATE method,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// your code for navigation.
}
First, you are using the method tableView:didDeselectRowAtIndexPath:, where it should be tableView:didSelectRowAtIndexPath:. Don't worry, that's something I keep doing from time to time.
Then, make sure your segue's identifier is "showInfo" in your storyboard and you are good to go.

tableview in normal view controller

I am doing the project which need to use the storyboard to rewrite the old project. I got some problems when I doing it use the storyboard. I use the normal view controller instead of tableview controller, because there are some buttons and labels in this view controller. The problem is I don't know how to link between this view controller and the detail view controller when users select the cell from the table view.
simply drag the tableview to a normal VC.
ctrl+drag from your table to the orange circle below to connect as delegate/datasource.
make your VC to implement uitableview delegate and datasource protocols (#interface MyVC : UIViewController <UITableViewDataSource, UITableViewDelegate>).
write the mandatory protocol methods:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{}
Select a cell in the Table View. And make a segue to the DetailViewController by ctrl clicking on the Table Cell and dragging to the DetailViewController in the Storyboard.
Then select a segue type based on the type of transition you want: modal, or push (if these view controllers are embedded in a navigation controller, etc.)
Probably you should use following code:
[self.navigationController pushViewController:[YourNewViewControllerClass new] animated:YES];

How to interact with UITableView in UIViewController?

I am addicted to use TableViewController in storyboard whose class directly inherits from UITableViewController and functions like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
are there by default and you just have to implement them. But now I am using a ViewController (inherits from UIViewController) in storyboard and using a UITableView control from Object library into the view controller by drag and drop. Obviously the class I would attach to my storyboard ViewController will be inherited from UIViewController. Now how do I interact with the table cells in the UITableView control in my view controller. Want to implement same row functions above.
Implement UITableViewDataSource and UITableViewDelegate protocols in your view controller .h file.
Have an outlet for the UITableView.
In viewDidLoad, set the tableView's datasource and delegate to self since the viewcontroller implements the protocol.
Then, write the cellForRowAtIndexPath method. It will be getting called.
You have to assign UITableViewDelegate and UITableViewDatasource protocols to your table and implement it. Methods in your question are methods from these protocols and they appears automatically when you use UITableViewController.

Resources