ThinkBonobo ThinkBonobo - 1 year ago 66
iOS Question

Running a method with Arguments after clicking a TableViewCell

I have a left-hand pane consisting of a table in a master-detail storyboard setup.

I want to be able to click the cells (dynamically created from a database) and have them perform 2 segues.

I created a method that I want to run.

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

//update nav
NSString *navSegue = [NSString stringWithString:@"Level1"];
[self performSegueWithIdentifier:navSegue sender:sender];

This method basically performs one segue that changes the master table to a sub table, and another segue that changes the detail to some other view.

My question is how can I set up my custom UITableViewCell so that when I click it, the method will run and I will be able to give method information about the cell (the custom cell hold some parameters such as 'id' which I do not display).

I tried turning it into a UIControl but that didn't work
I also tried using UITapGestureRecognizer but it would not let me add arguments.

SEL cellSelector = @selector(cellClicked);
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:cellSelector];
[cell addGestureRecognizer:tap];

Any suggestions on making it clickable or more optimal methods of solving the situation would be much appreciated. Thanks for your time.

==== EDIT =====

I tried didSelectRowAtIndex path but it seems that I get an odd error:

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.'

Below is what I wrote for didSelectRowAtIndexPath:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
EventCell* sender = [tableView dequeueReusableCellWithIdentifier:@"EventCell" forIndexPath:indexPath];
[self cellClicked:sender];

Below is my EventModel class:

#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;


and the .m

#import "EventModel.h"

@implementation EventModel

-(id)init {
return [super init];


The problem resided in the fact that I was using "dequeueReusableCellWithIdentifier" instead of "cellForRowAtIndexPath"…

Answer Source

The tableView has a delegate method tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath that will give you the index of the selected row. With that you could perform any desired actions.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download