Ios 单击TableViewCell后运行带参数的方法

Ios 单击TableViewCell后运行带参数的方法,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个左侧窗格,由主细节情节串联板设置中的一个表组成 我希望能够单击单元格(从数据库动态创建),并让它们执行2个步骤 我创建了一个要运行的方法 -(IBAction)cellClicked:(id)sender { //update detail screen [self performSegueWithIdentifier:@"showItemOrGroup1" sender:sender]; //update nav NSString *navSegue

我有一个左侧窗格,由主细节情节串联板设置中的一个表组成

我希望能够单击单元格(从数据库动态创建),并让它们执行2个步骤

我创建了一个要运行的方法

-(IBAction)cellClicked:(id)sender {
    //update detail screen
    [self performSegueWithIdentifier:@"showItemOrGroup1" sender:sender];

    //update nav
    NSString *navSegue = [NSString stringWithString:@"Level1"];
    [self performSegueWithIdentifier:navSegue sender:sender];
此方法基本上执行一个将主表更改为子表的步骤,以及另一个将细节更改为其他视图的步骤

我的问题是如何设置自定义UITableViewCell,以便在单击它时,该方法将运行,并且我将能够提供有关该单元格的方法信息(自定义单元格包含一些参数,例如我不显示的“id”)

我试着把它变成一个UIControl,但没有成功 我还尝试使用UITapGestureRecognitzer,但它不允许我添加参数。 e、 g

任何关于使其可点击或解决问题的更优化方法的建议都将不胜感激。谢谢你抽出时间

==编辑=====

我尝试了didSelectRowAtIndex path,但似乎出现了一个奇怪的错误:

2013-12-12 11:38:45.974 Catsy[4772:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[EventModel<0x92ab0> init]: cannot init a class object.'
下面是我的EventModel类:

#import <Foundation/Foundation.h>

@interface EventModel : NSObject

@property (nonatomic) NSInteger eventId;
@property (nonatomic) NSInteger parentId;
@property (nonatomic) NSInteger rootId;
@property (strong, nonatomic) NSString *name;
@property (nonatomic) NSInteger sequence;
@property (nonatomic) NSInteger levelNo;
@property (nonatomic) NSInteger createdate;
@property (nonatomic) NSInteger updatedate;
@property (strong, nonatomic) NSMutableArray *childrenIds;

/* =INSTANCE METHODS= */
-(id)init;
@end
==解决方案的澄清====


问题在于我使用了“dequeueReusableCellWithIdentifier”而不是“CellForRowatineXpath”…

tableView有一个委托方法
tableView:(UITableView*)tableView didSelectRowatineXpath:(NSIndexPath*)indexPath
,它将为您提供所选行的索引。有了它,您可以执行任何想要的操作。

我的两分钱:

a) 如果您使用的是storyboard/segue,运行时将调用您在storyboard中设置的segue,并在“didSelectRowatineXpath”之后调用:因此请注意顺序

b) 如果你留在segue,你可以用“PrepareForsgue”来准备

c) 您最终可以阻止调用segue对“shouldPerformSegueWithIdentifier”的“否”响应,并使用didSelectRowAtIndexPath中的“performSegue..”手动调用segue,并使所有需要的逻辑。。。
设置视图、按钮,然后执行序列或手动按下控制器。

谢谢!我试过了,但它给了我一个错误。请看我上面的更新。你能补充一下你的两个赛格在做什么吗?除非您需要更改手机的外观,否则您不必返回手机,您应该在
didselectrowatinepath
中进行操作。否则,您应该只使用索引来更新/查询您的模型。另请注意,
dequeueReusableCellWithIdentifier
用于创建新单元格,不应用于访问单元格。如果您需要访问单元格,请使用
cellforrowatinexpath
。Rufel,非常感谢!答案与你的旁注有关。当我使用cellForRowAtIndexPath时,我的代码工作得非常出色。你是救命恩人。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    EventCell* sender = [tableView dequeueReusableCellWithIdentifier:@"EventCell" forIndexPath:indexPath];
    [self cellClicked:sender];
}
#import <Foundation/Foundation.h>

@interface EventModel : NSObject

@property (nonatomic) NSInteger eventId;
@property (nonatomic) NSInteger parentId;
@property (nonatomic) NSInteger rootId;
@property (strong, nonatomic) NSString *name;
@property (nonatomic) NSInteger sequence;
@property (nonatomic) NSInteger levelNo;
@property (nonatomic) NSInteger createdate;
@property (nonatomic) NSInteger updatedate;
@property (strong, nonatomic) NSMutableArray *childrenIds;

/* =INSTANCE METHODS= */
-(id)init;
@end
#import "EventModel.h"

@implementation EventModel

-(id)init {
    return [super init];
}
@end