user3397502
user3397502

Reputation: 29

Go from UITableView(not a UITableViewController) to a UIViewController

I have a UITableView inside a UIViewController.
When the user presses a cell inside the UITableView I would like to show another UIViewController. So I have UIViewController "A", with UITableView "A" inside and I would like to click a cell in UITableView "A" and be pushed to UIViewController "B". I tried simply connecting a segue from the UITableViewCell to the UIViewController but this results in nothing happening when the cell is pressed. I also tried the following code unsuccessfully. Again, nothing happening, no errors.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
    AlbumsViewController *albumsViewControllerObj = [mainStoryboard instantiateViewControllerWithIdentifier:@"AlbumsViewController"];
   [albumsViewControllerObj performSegueWithIdentifier:@"showAlbumDetail" sender:self];

}

All of the advice I have found so far involves going from a Table View Controller to another view, which I know how to do and have never had a problem with. For some reason though I am having issues moving from a Table View to another View Controller. If it matters (and I think it may) the delegate and datasource of the TableView "A" is View Controller "A". Any advice would be great, thank you!

Upvotes: 1

Views: 189

Answers (3)

Nischal Hada
Nischal Hada

Reputation: 3288

Check first Any chance you accidentally typed didDeselectRowAtIndexPath? .Check out if the method name of what you expect of being didSelect may accidentally be gotten didDeselect in some way.

in the SimpleTableController.h

#import <UIKit/UIKit.h>

@interface SimpleTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@end

In SimpleTableViewController.m .check if didSelectRowAtIndexPath is called or not by doing as below

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UIAlertView *messageAlert = [[UIAlertView alloc]
                                        initWithTitle:@"Row Selected" message:@"You've selected a row" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

        // Display Alert Message
        [messageAlert show];

    }

set the delegate and datasource .

enter image description here

Upvotes: 0

Jessica
Jessica

Reputation: 9830

Set up the delegate and datasource, either programmatically like this:

- (void)viewDidLoad {
    ...
    self.myTableView.delegate = self;
    self.myTableView.datasource = self;
}

Or you can do it in the storyboard by ctrl click and drag from your tableView to the viewController.

Then you can segue to viewController b, programmatically or in the storyboard.

Porgrammatically:

First go to the storyboard and ctrl click and drag from viewController a to viewController b. Then in your code write the following:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self  performSegueWithIdentifier:@"nextViewControllerID" sender:nil];
}

StoryBoard:

ctrl click and drag from the cell to viewController b.

Upvotes: 0

Leo
Leo

Reputation: 24714

First,set the delegate and datasource to self.

Second,just drag a segue from ViewControllerA to ControllerB

Third,just perform segue when select

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  [self  performSegueWithIdentifier:@"showAlbumDetail" sender:nil];
    }

Then,in prepareForSegue,pass data to destinnatinVC

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if (segue.identifier == yourid) {
        ViewControllerB *  = segue.destinationViewController;
        //Pass data
    }
}

Upvotes: 2

Related Questions