Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to segue from a UITableViewCell on a UIView to another view

Xcode 4.6.1 iOS 6 using storyboards

My problem is this

I have a UITableView with dynamic prototype cells on a UIView in a UIViewController (that is itself embedded in a navigation controller) and I want to segue from one specific cell to another view

(Before anyone suggests I should just be using a UITableViewController , I do have other things on the UIView, so i'm set up this way for a reason.)

Now i'm not sure how to go about creating the segue

If I drag from the prototype UITableViewCell to create a segue , all the generated cells automatically call the the segue - when i need only one to do so. This is normal behaviour and I would get around this if i was using a UITableViewController by creating the segue by dragging from UITableViewController and calling [self performSegueWithIdentifier:.... From my didSelectRowAtIndexPathMethod so only the specific cell I want to perform this segue triggers it.

I don't have a UITableViewController in this case - just my UITableView on a UIView that is part of a UIViewController subclass

I've been playing around and I have just discovered that i cannot drag from the UITableView - doesn't let you do that, so that was a deadend.

My only choice that seemed left to me was to drag from the UIViewController

So i tried that and of course XCode throws up an error on the perform segue line telling me i have ... No visible interface for 'LocationTV' declares the selector performSegueWithIdentifier. LocationTv being my tableview subclass.

What is the correct way to attempt to call the new view in this situation

Thank

Simon

like image 687
SimonTheDiver Avatar asked Jun 14 '13 07:06

SimonTheDiver


People also ask

How do I segue from UITableViewCell?

So, select the first view controller, and then go to the top menu and click on Editor → Embed In → Navigation Controller. For this, we are going to select the “show” segue in the “Selection Segue” section. This means that when you tap on the cell, it will cause this segue to happen.

Can I use UITableViewCell as a view?

There is one way you can use contentView property of UITableViewCell which of type UIView . So simply get RestaurantTableViewCell as you previously getting from nib then use its contentView to get the UIView from it.

How do I create a segue between view controllers?

To create a segue between view controllers in the same storyboard file, Control-click an appropriate element in the first view controller and drag to the target view controller. The starting point of a segue must be a view or object with a defined action, such as a control, bar button item, or gesture recognizer.


1 Answers

First of all segues can be use only between UIViewControllers. So in case you want to perform a segue between two views that are on the same view controller, that's impossible.

But if you want to perform a segue between two view controllers and the segue should be trigger by an action from one view (inside first view controller) well that's possible.

So in your case, if I understand the question, you want to perform a segue when the first cell of a UITableView that's inside of a custom UIView is tapped. The easiest approach would be to create a delegate on your custom UIView that will be implemented by your UIViewController that contains the custom UIView when the delegate method is called you should perform the segue, here is a short example:

YourCustomView.h

@protocol YourCustomViewDelegate <NSObject>

-(void)pleasePerformSegueRightNow;

@end

@interface YourCustomView : UIView {
   UITableView *theTableView; //Maybe this is a IBOutlet
}
@property(weak, nonatomic) id<YourCustomViewDelegate>delegate;

YourCustomview.m

@implementation YourCustomview
@ synthesise delegate;

//make sure that your table view delegate/data source are set properly
//other methods here maybe

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == 0) {  //or any other row if you want
       if([self.delegate respondsToSelector:@selector(pleasePerformSegueRightNow)]) {
          [self.delegate pleasePerformSegueRightNow];
       }
    }
} 

YourTableViewController.h

@interface YourTableViewController : UIViewController <YourCustomViewDelegate> {
  //instance variables, outlets and other stuff here
}

YourTableViewController.m

@implementation YourTableViewController

-(void)viewDidLoad {
  [super viewDidLoad]; 
  YourCustomView *customView = alloc init....
  customView.delegate = self;
}

-(void)pleasePerformSegue {
  [self performSegueWithIdentifier:@"YourSegueIdentifier"];
}

You can create any methods to your delegate or you can customise the behaviour, this is just a simple example of how you can do it.

like image 174
danypata Avatar answered Dec 24 '22 12:12

danypata