UITableView in a View controller by a UIViewController and not a UITableView - uitableview

I have a View with lots of things inside it including buttons, a scroll view and a tableView (ipad app). I am controller this view with a viewController subclass but I don't know how to manage my tableView. I don't know where put the methods :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
should I add them to my ViewController or should I create a new subclass of UITableViewController (and get them "for free") and set the dataSource and delegate of my tableView to that class when I create it programmatically?
I am storing the data I want to show in my appDelegate
.

You just have to set the UIViewController as the delegate and dataSource of your UITableView. That's it. You don't need to create a own subclass of UITableViewController for managing that tableView. So you can put all the subviews in the viewController's view.

Thank you so much, I got it. I was trying to create a tableView and then set it's datasource. In fact the easier way is to create a tableViewController and use it's .view property that is already linked to it and use the addSubview method to put it in the main viewController's view.
Cheers

Related

UITableView inside UITableViewCell using Interface Buillder

I have an tableView called BasicTable that have custom cell called CustomCell and I want to add a new table view inside this CustomCell.
I have an BasicTableView.m for BasicTable and CustomCell.m for CustomCell and add the new table view using Interface Builder but now don't know where to go or where to add reference to the new tableView and it's new cell.
You can create a reference to the new tableView directly in CustomCell.m (e.g. by setting it as a property) and set the cell itself als dataSource and delegate of the tableView.
Having another tableView inside of a table view cell however doesn't seem like a good design decision, this will lead to problems in UX (e.g. concerning the scrolling of the two). Did you consider using sections inside the table view instead?
Also, if you use my above solution (which I wouldn't recommend because of the UX issues), be sure to update the tableView inside the table view cell, this should best be done in the table view cell's method prepareForReuse, it could look like this:
- (void)prepareForReuse
{
[super prepareForReuse];
[self.tableView reloadData];
}
And do this only after having updated the cell in the table view controller in cellForRowAtIndexPath:
You should connect your new tableview to your CustomCell and set it as datasource and delegate, and implement that protocols in you CustomCell.
i.e. your BasicTable controls by your ViewController, but new tableview controls by CustomCell.

Static Table in a UIViewController

Pior Xcode 5.1, I had a Static Table view with two cells. I was simply using it in as the login form, username and password.
Now that I upgraded to Xcode 5.1 I am getting an error:
Static table views are only valid when embedded in UITableViewController instances
I found a few work arounds but they were all old a none of them helped me. My View Controller class is a subclass of the UIViewController class. I am also implementing the following two methods:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
switch (indexPath.row) {
case 0:
cell = self.tableViewCellUser;
break;
case 1:
cell = self.tableViewCellPassword;
break;
}
return cell;
}
This was working perfectly, but for some reason Xcode 5.1 broke it... Using a container seem to be a overkill as I would need a new view controller just because of two properties...
Static cells are only valid in a UITableViewController. Not in a UITableView.
If you used the UITableview instead of the UItableViewController because the layout shouldn't be fullscreen than add a container view to the view you want to place it instead of the table view in the storyboard.
Create a tableViewController in the storyboard.
Control drag from the containerView to te TableViewController and release. select 'embed' from the popup.
I am also implementing the following two methods:
Are you sure that your code included those methods when it was working? If you're using a static table, you shouldn't implement any of the data source methods. Look, it says so right here:
Note: If a table view in a storyboard is static, the custom subclass of UITableViewController that contains the table view should
not implement the data source protocol. Instead, the table view
controller should use its viewDidLoad method to populate the table
view’s data.
I had similar trouble myself recently -- I created a static table and found that it didn't work (cells didn't show up properly) because I had inadvertently implemented the same methods that you have.
Also, as some of the comments indicate, you should be using an instance of UITableViewController or a subclass. It's hard to see why this might be a problem for you -- you can do anything in a table view controller that you can do in a regular old view controller.

How to use a TableView inside a UIViewController?

I want to add a simple static TableView to my UIView. So i dragged it into my UIView. Added the TableViewDataSource and Delegate Protocols to my ViewController class. Created an outlet to my tableview and connected the datasource and delegate outlets to the viewcontroller.
But i still get an error message which says that Static table views are only valid when embedded in UITableViewCOntroller Instances ? Any Ideas how to solve this Problem ?
PS: I am using UIStoryboards for designing the UI
Use a containerView (drag one onto your UIView) which will link to a UITableViewController instead. You will use the prepareForSegue method to initialize the table view controller.
Oh, and you might want to accept answers that help you or no one will help you anymore.
If you want static cells, in your Storyboard, click on the tableview, then in the inspector, change the Content from 'Dynamic Prototypes' to Static Cells. You can also segue these cells to a new controller without having to set up data sources and delegates for the table.

Setting the subclass of a UITableViewController

I have created a project where I have a UITableViewController with static cells. I have created a UITableViewController subclass so that I can write the code for some of the controls I want to put in the static cells - however when I set the custom class of the UITableViewController to that of the subclass I've created, the table isn't displayed. Coudl anyone please help me with what to do that the table will be displayed after I've set the custom class of the UITableViewController?
Thanks
Assuming you are using storyboard and static cells: make sure the datasource methods in your controller do are not overriding the number of sections and rows in the table view. With static cells you can delete both numberOfRowsInSection: and numberOfSections.

Actionsheet instead of segue when clicking on uitableviewcell

Here's my ultimate goal in all of this. I have a viewcontroller with a table added to the view. I've set the cells to static, and I'm going to place the selected options in the cell. I want an actionsheet with a picker view on it. I have several arrays on the view which contain the various options. When someone clicks the cell I want it to call a method which checks which cell was click, passes the picker view the correct array of options to display and shows the action sheet.
I've manually added a table to the view using the storyboard, but when I ctrl drag from a cell it only gives me the option to add a segue. How do I set it so the cell click just goes to a method I create?
See UITableViewDelegate
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- tableView:willSelectRowAtIndexPath and - tableView:didSelectRowAtIndexPath: should work. Did you try them? Your viewController needs to adopt UITableViewDelegate.

Resources