0

I am in situation where i need to call didSelectRowAtIndexPath from another calls for that i do like this i make object of that class and call viewdidload so my table view get initialised

- (void)_hideTapped:(id)sender 
{
    LeftController *left = [[LeftController alloc]init];
    [left viewDidLoad ];   
} 

in LeftController

- (void)viewDidLoad {
    [super viewDidLoad];
    mainArray = [[NSMutableArray alloc]initWithObjects:@"Go To Camera",@"Big Contacts List",@"Where Am I? On map",@"Watch And Alarm",@"Calculator With Big Letters",@"Big KeyBoard For Email",@"Calendar With Events",@"Settings", nil ];

 //   NSLog(@"mainArray%@",mainArray);

    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        tableView.delegate = (id<UITableViewDelegate>)self;
        tableView.dataSource = (id<UITableViewDataSource>)self;
        [self.view addSubview:tableView];
        self.tableView = tableView;

         [self iDecideWhichCell];
    }
}


-(void)iDecideWhichCell
{    
    NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0];

    [self tableView:self.tableView didSelectRowAtIndexPath:path];  
}

this is part of my didSelectRowAtIndexPath i am using JASidePanels for splitview like facebook and this is how they push viewcontroller it works fine if i go to leftcontroller and click by my self but when i do this whole process programmatically then its not working

if (indexPath.row ==0) 
{
    NSLog(@"%@",tableView);
    self.sidePanelController.centerPanel = [[UINavigationController alloc] initWithRootViewController:[[NewViewController alloc] init]];     
}

please any one can tell me how can i achieve this i checked this answer calling didSelectRowAtIndexPath from other ViewController not responding iPad

but i dont understand how to implement this in my code

Community
  • 1
  • 1
java
  • 239
  • 5
  • 20
  • give delegate and datasource for that class , such like tableView.delegate = youClassName; and tableView.datasource = youClassName; –  Aug 23 '13 at 06:27
  • That's a delegate method - don't call it directly. Call `[object_holding_tableView.tableView selectRowAtIndexPath:...animated:NO];` You will need a reference to that object that holds your table view. – Rok Jarc Aug 23 '13 at 06:27
  • can you please answer in detail i am so new to this all will be great help – java Aug 23 '13 at 06:30
  • @RanjuPatel as you can see in view did load delegate and datasource is there is it not ok? – java Aug 23 '13 at 06:33
  • @rokjarc i did like this NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0]; [[self.tableView delegate] tableView:self.tableView didSelectRowAtIndexPath:path]; but its not working too – java Aug 23 '13 at 06:34
  • @java: in that case you probably didn't set your `tableView.delegate` or it got deallocated. – Rok Jarc Aug 23 '13 at 06:35
  • @rokjarc NSLog(@"%@",[self.tableView delegate]); NSLog(@"%@",[self.tableView dataSource]); this get print like this – java Aug 23 '13 at 06:39

2 Answers2

3

tableView:didSelectRowAtIndexPath: is a delegate method. You shouldn't call it directly. It is called for you when a table view row is selected.

You should create a delegate for the LeftController, so the LeftController can remain as the delegate of its own tableView.

Implement in LeftController.h:

@class LeftController;

@protocol LeftControllerDelegate
-(void)leftController:(LeftController *)leftController didSelectTableView:(UITableView *)tableView rowAtIndexPath:(NSIndexPath *)indexPath;
@end

@interface LeftController : UIViewController
@property(nonatomic, weak) id<LeftControllerDelegate> delegate;
// ... other public properties
-(id)initWithDelegate:(id<LeftControllerDelegate>)delegate;
// ... other public methods
@end

Implement in LeftController.m:

-(id)initWithDelegate:(id<LeftControllerDelegate>)delegate
{
    self = [super init];
    if (self) {
        self.delegate = delegate;
    }
    return self;
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    //create the tableView...

    //set the tableView delegate
    tableView.delegate = self;

    //do other view setup...
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate leftController:self didSelectTableView:tableView rowAtIndexPath:indexPath];
}

In your other view controller implementation, e.g. MyOtherController.m:

//create instance of LeftController that has self as delegate
LeftController *left = [[LeftController alloc] initWithDelegate:self];

and implement the delegate method:

-(void)leftController:(LeftController *)leftController didSelectTableView:(UITableView *)tableView rowAtIndexPath:(NSIndexPath *)indexPath
{
    //Yay! This instance of MyOtherController has received the delegate message from LeftController, including details of table view and row selected.
}

Effectively here, you have a delegated the tableView messages to the LeftController, and in turn you've delegated the LeftController functionality to its delegate. You set its delegate in the init method when you created and initialised it.

Hope it makes sense, let me know!

Sam
  • 5,892
  • 1
  • 25
  • 27
  • How to make my knowledge of delegage like your knowledge what you will recommend to read or do? – java Aug 23 '13 at 07:58
  • I've recommended this blog post before, it's http://alexefish.com/post/15966868557/understanding-and-creating-delegates-in-objective-c very good explanation. Apple's own docs are also good: https://developer.apple.com/library/ios/documentation/general/conceptual/DevPedia-CocoaCore/Delegation.html and https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html – Sam Aug 23 '13 at 09:31
0

You can make your own method in which class you want to handle didSelectRowAtIndexPath and call that method from didSelectRowAtIndexPath of same class. Also Set delegate to handle events.

Nishith Shah
  • 523
  • 3
  • 16
  • can you please put some code for this how to do this as am so new to this all will be great help – java Aug 23 '13 at 06:31