Create an event (IBAction) from a cell - ios

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

Related

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.

Segue to current view UItableView

I have an undefined number of levels (depth) in my tableview, so i'd like to be able to create a segue between the view and itself so that i can call it as long as i have sub levels to discover. How can i do that ?
I've thought of directly using [self.navigationView pushViewController] when i need it, but i'd like the new controller to use the defined view in my SB.
Thanks,
EDIT: For now the only solution i've found is duplicating the VC, setting a segue between VC1 and VC2 called 'showNext', and a segue between VC1 and VC2 called the same way. Isn't there a better solution ?
If you're using dynamic cell prototypes, you obviously can do a segue from the table view cell to the controller without any problem.
When you make your segue, you end up with:
But let's imagine for a second that there's some reason that doesn't work for you, e.g. you could not have a segue from the cell (for example, you need to invoke segue from didSelectRowAtIndexPath, not upon selecting the cell). In this case, you couldn't use that previous technique.
There are a couple of options in this case:
As pointed out by Chris, if only supporting iOS 6 and above, use the above technique, but (a) make sure your didSelectRowAtIndexPath uses self for the sender and then have shouldPerformSegueWithIdentifier only allow the segue if the sender == self (thus when the sender is the table view cell, it will be canceled);
Perhaps even easier, just don't define a segue at all and have your didSelectRowAtIndexPath manually instantiateViewControllerWithIdentifier and then push/present that view controller as appropriate; or
You can use the following, kludgy work-around: In short, add a button to your view controller (drag it down to the bar at the bottom), where it won't show up on the view, but you can use its segues. So first, drag the rounded button to the controller's bar at the bottom of the scene:
Now you can make a segue from that button to your view controller:
And thus, you end up with the self-referential segue again:
You must give that segue an identifier, so you can invoke it programmatically via performSegueWithIdentifier, but it works. Not the most elegant of solutions, but I think it's better than having an extra scene on your storyboard.
None of these are ideal, but you have lots of options.
This process worked for me to do the exact same thing...
Set up your tableview in storyboard as usual. DO NOT give your prototype cell a Cell Identifier. Set up your segue as you did above, by dragging from the cell to the controller.
In your viewDidLoad register the cell class
//register the cell identifier as we are not loading form a storybard
[_aTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"CellID"];
In your cellForRowAtIndexPath set up your cell
static NSString *CellIdentifier = #"CellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Now you can use didSelectRowAtIndexPath and call performSegueWithIdentifier. the prepareForSegue will only be called once.

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.

Issues using custom iOS UITableView cells / "Multiple segues with identifier" error

I have created a Table View that has multiple Table View Cells of different styles - say "CellStyle1" and "CellStyle2".
The cells look different, but they should perform the same when selected - namely, segueing to the same new view.
I have set up both cells to have the same Storyboard Segue - "PushView", pointing to the same view controller.
This seems to work OK, but Xcode generates a warning: "Multiple segues with identifier"
How can I avoid this error? What is the right way to handle multiple custom cells that look differently but act the same and should segue to the same place?
This one has me scratching my head.
Thanks!
You could make one segue by ctrl dragging from the view controller to the destination view controller. And then assuming tapping the cell is the event that you want to trigger the segue, in didSelectRowAtIndexPath call performSegueWithIdentifier.
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender
Or just give the 2 segues different identifiers and in prepereForSegue check for either identifier.

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