Static Table in a UIViewController - ios

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.

Related

Create an event (IBAction) from a cell

After all my searching, I didn't find any specific informations/questions on that one : I want to link a whole Table View Cell (like if it was a button) to an IBAction (which, in my case, will perform a segue).
The thing is that I apparently can't Ctrl-Drag the Cell to my code to creat an event (since there is no category for that in my connections inspector, in the storyboard). This last point makes me fear that it isn't possible.
I managed to create a segue, directly from the Cell, to another view but it gets triggered everytime I touch the Cell. And I need to have a condition on that segue, which is why I have a segue from my Table View (containing the said Cell) to the Target View. I then created an IBAction which calls the segue (Table View to Target View).
How do I link my Cell to that Segue?
PS: I can edit my question for the code, but we only have the IBAction in my TableViewController (declared as Class on my Table View in the storyboard) and then the Segue which was created using the Storyboard.
My Table is static (as for now, it's for testing).
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
There are many way to achieve this, if you just wanted a segue you can do this without code
if not do it like #waki
Cell To UIViewController:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

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.

IOS SplitView TableViewController query

I have one SplitViewController with default settings added into an xib file. Its Table View controller i defined as another class (in Identity Inspector) as a TableView Controller class I created.
Now I have put some code in its
...numberOfRowsInSection:(NSInteger) section
and ...cellForRowAtIndexPath:(NSIndexPath *) indexPath
It works fine with total values in its table according to numberOfRows I have defined and elements in cell according to what cells I create and return in the appropriate method.
My question is:
How is table coming into the SplitView because when I customize UITableView in the xib of my TableViewController, its changes do not appear in the SplitView's left section. (I guess that table View is non-customizable? )
Secondly How does iOS initialize that TableViewController class from XIB because I have to put some initialization code that when that TableView is created, do following things. I tried over-riding various init functions it does not call any of those. How can I initialize some data when that TableViewController is initialized?
Thanks.
The table view is customized in the XIB that contains the split view and not in an own XIB file
You can do 2 things: override initWithCoder: (remember to use the self=[super initWithCoder: pattern) or override - (void)awakeFromNib

UITableView in a View controller by a UIViewController and not a 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

Resources