0

I am developing an application in which I am having common structure of UITableView for all my UIViewControllers. So I have created a UITableView subclass containing common structure, then I have added this view to my UIViewController.

UITableView is displaying properly but I need to addTarget to UIButton added to Custom UITableViewCell from my ViewController and method is written in UIViewController.

Can anyone please tell me how to achieve this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Shital Tiwari
  • 175
  • 2
  • 3
  • 17
  • are you using **storyboard** or the old system ? if storyboard, it's possible this may help greatly http://stackoverflow.com/a/23060746/294884 – Fattie May 26 '14 at 09:01
  • @JoeBlow: Yes I am using Storyboard – Shital Tiwari May 26 '14 at 09:03
  • @JoeBlow I tried to do it by using container view controller but this is not working as per my requirement as on `UIButton` click my table height is changing – Shital Tiwari May 26 '14 at 09:47
  • @ShitalTiwari : Kindly go for `NSNotificationCenter` as described in my answer. There is a sample code link that explains the usage of `NSNotificationCenter` in a custom sub-class. – Balram Tiwari May 28 '14 at 05:04

3 Answers3

1

You need to work on your Program design to get this problem sorted. However you have come far enough to go back & change your design. So for you the better option is the NSNotificationCenter.

For your case, do this:

  1. In your viewController where your method is written, lets say method name is myX-Method: (which performs the button's target functionality), then in the viewDidLoad of this controller just add this line.

    -(void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myX-Method:) name:@"NOTIFY_CUSTOM_BUTTON" object:nil];
    }
    
  2. Now, in your UITableViewCell class, set a local method as buttons target as normally we do, lets say , myLocalMethod:

  3. Now in this local method, post the notification message like this. For example I am just passing the current cell's button text as message to notification.

    -(void) myLocalMethod:(UIButton *)button{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFY_CUSTOM_BUTTON" object: button.title];
    }
    

This way you need not have to be dependent on delegate or so. Use notification object to collect your values in myX-Method: like this :

-(void)myX-Method:(NSNotification *)dict {
    NSString *buttonTitle = [dict valueForKey:@"object"];
    NSLog(@"Button Clicked : %@", buttonTitle);
}

Hope this simple solution will solve your purpose.

Refer here for a sample code. However in example code project, kindly search NSNotificationCenter

Balram Tiwari
  • 5,657
  • 2
  • 23
  • 41
0

you can set the cell delegate as the view controller at tableView:cellForRowAtIndexPath:

then when a button is pressed inside the cell you can trigger the delegate method inside the buttonPressed: method in the cell.

so the cell calls cell.delegate which is the viewController

in CellView.h

@property (nonatomic, weak) id delegate;

in CellView.m

- (void)buttonPressed:(id)sender {
    [self.delegate cellButttonPressed:sender];
}

in MyTableViewController.m

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    CellView *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.delegate = self;
    return cell;
}

- (void)cellButttonPressed:(id)sender {
//button action code here
}
Tomer Even
  • 4,820
  • 2
  • 30
  • 36
0

Use Delegate Methods For Example

Make SubClass of UITableViewCell

Declare Delegate Methods

  @protocol  CustomCellDelegate <NSObject>

-(void)btnClikced;

@end

// InterFace

@interface CustomCell : UITableViewCell

@property(nonatomic,retain) id<CustomCellDelegate> delegate;

//implementation

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeContactAdd];
        [btn addTarget:self action:@selector(btnTapped) forControlEvents:UIControlEventTouchUpInside];
        self.accessoryView=btn;
    }
    return self;
}
-(void)btnTapped{
    [self.delegate btnClikced];
}

in your ViewController

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.delegate = self;
return cell;

}

- (void)btnClikced{
//button action code here
}
Sunny Shah
  • 12,990
  • 9
  • 50
  • 86