【问题标题】:how to call didSelectRowAtIndexPath from another class如何从另一个类调用 didSelectRowAtIndexPath
【发布时间】:2016-03-18 18:42:59
【问题描述】:

我处于需要从另一个调用中调用 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 这样的 splitview,这就是他们如何推动 viewcontroller

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

请任何人告诉我如何实现这一目标 我检查了这个答案calling didSelectRowAtIndexPath from other ViewController not responding iPad

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

【问题讨论】:

  • 为该类提供委托和数据源,例如 tableView.delegate = youClassName;和 tableView.datasource = youClassName;
  • 这是一个委托方法 - 不要直接调用它。致电[object_holding_tableView.tableView selectRowAtIndexPath:...animated:NO]; 您将需要对包含您的表格视图的对象的引用。
  • 你能详细回答吗?我对这一切都很陌生
  • @RanjuPatel 正如您在视图中看到的那样,加载委托和数据源是否存在问题?
  • @rokjarc 我确实喜欢这个 NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0]; [[self.tableView 委托] tableView:self.tableView didSelectRowAtIndexPath:path];但它也不起作用

标签: iphone ios didselectrowatindexpath


【解决方案1】:

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 方法中设置它的委托。

希望有道理,让我知道!

【讨论】:

【解决方案2】:

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

【讨论】:

  • 能否请您输入一些代码来说明如何做到这一点,因为我对这一切都很陌生
猜你喜欢
  • 1970-01-01
  • 2014-05-02
  • 2021-08-03
  • 1970-01-01
  • 1970-01-01
  • 2014-05-21
  • 2017-05-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多