-3

I have a standard method, that forms the cell inside table:

- (SearchTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.button.tag = 3;
}

There is button inside cell with action:

- (IBAction)changeName:(id)sender {
    // GOTO
}

How to change the name of the button after I clicked on it

Larme
  • 24,190
  • 6
  • 51
  • 81
  • 1
    `UIButton *button = (UIButton *)sender; if (button.tag == 3){//set button title}`? – Larme Mar 01 '16 at 16:29
  • @Larme, that's probably the whole answer. You care to make that an answer? :) – Tobi Nary Mar 01 '16 at 16:34
  • Possible duplicate of [Objective C: what is a "(id) sender"?](http://stackoverflow.com/questions/5578139/objective-c-what-is-a-id-sender) – Larme Mar 01 '16 at 16:36

2 Answers2

0

Try it :

- (IBAction)changeName:(id)sender {
    UIButton *button = (UIButton *)sender;

    if (button.tag == 3) {
        [button setTitle:@"Your title" forState:UIControlStateNormal];
    }
}

Don't forget to link your method to your button in the nib.

Pipiks
  • 2,018
  • 12
  • 27
-1

Using protocol Delegate method
Code for custom cell .h file

#import <UIKit/UIKit.h>
@protocol CustomCellProtocal <NSObject>
 -(void)CellButtonClicked:(UIButton *)sender;
@end

@interface CustomCell : UITableViewCell
   @property(weak) id<CustomCellProtocal>delegate;
   - (IBAction)BtnClicked:(id)sender;
@end


code for custom cell .m file

#import "CustomCell.h"

@implementation CustomCell
@synthesize delegate;
  - (IBAction)BtnClicked:(id)sender {
      [delegate CellButtonClicked:sender];
   }
@end


Another simplest way
I suggest you to create custom cell having Button. Create outlet for that button in custom cell class.

Then in cellForRowAtIndexpath() add target to that button like follow

[CustomCell.buttonOutlet addTarget:self action:@selector(BtnClicked:) forControlEvents:UIControlEventTouchUpInside];


Then handle your logic in BtnClicked method.

-(void)BtnClicked:(UIButton *)sender{
     if(sender.tag == 1){
        [sender setTitle:@"Hello" forState:UIControlStateNormal];
 }
}


I strongly suggest, second way is best for your case. And cell will be dequed with identifier. so no need to worry.

Avinash Jadhav
  • 491
  • 4
  • 17
  • That is horrible, not only is this not anwering the question at all, nor does it adhere to code conventions. Also, this adds a new target on every call to `tableView:cellForRowAtIndexpath:`, so the button may fire the action(s) more than once. – Tobi Nary Mar 01 '16 at 16:42
  • Actually cell is dequeReusableCellWithIdentifier.... Secondly by setting tag to each button as of indexPath.row we can identify each button of the record and perform login in same TargetAction method – Avinash Jadhav Mar 01 '16 at 16:48