سؤال

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

هل كانت مفيدة؟

المحلول

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!

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top