0

我处于需要从另一个调用中调用 didSelectRowAtIndexPath 的情况,因为我确实这样做了,我创建了该类的对象并调用 viewdidload 以便我的表视图被初始化

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

在左控制器

- (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];  
}

这是我的 didSelectRowAtIndexPath 的一部分,我正在使用 JASidePanels 进行像 facebook 这样的拆分视图,这就是他们推动 viewcontroller 的方式,如果我转到 leftcontroller 并自行单击,它可以正常工作,但是当我以编程方式执行整个过程时,它就无法正常工作

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

请任何人都可以告诉我如何实现这一点我检查了这个答案,从其他 ViewController 调用 didSelectRowAtIndexPath 没有响应 iPad

但我不明白如何在我的代码中实现这一点

4

2 回答 2

3

tableView:didSelectRowAtIndexPath: 是一个委托方法。你不应该直接调用它。It is called for you when a table view row is selected.

您应该为 LeftController 创建一个委托,以便 LeftController 可以保留为它自己的 tableView 的委托。

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

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];
}

在您的其他视图控制器实现中,例如MyOtherController.m

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

并实现委托方法:

-(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.
}

实际上,您将 tableView 消息委托给了 LeftController,而您又将 LeftController 功能委托给了它的委托。当您创建和初始化它时,您在 init 方法中设置它的委托。

希望它有意义,让我知道!

于 2013-08-23T06:48:09.660 回答
0

您可以在要处理的类中创建自己的方法,didSelectRowAtIndexPath并从同一类的 didSelectRowAtIndexPath 调用该方法。还设置委托来处理事件。

于 2013-08-23T06:29:07.777 回答