Use an IBAction from a UIButton inside a user cell in the main view controller

I created a custom cell with my own .m, .h and .xib files. In the cell, I have a UIButton, which I added to xib in IB.

I can get IBAction from UIButton in this user .m cell, but in fact, I would like this redirect to click on the main view .m that the table is located on (and therefore the user cell), and use the action there.

I spent the last 4 hours on various ways to do this - should I use NSNotificationCenter? (I tried a lot of notifications, but I can't get it to work and I'm not sure that I have to be persistent)

+7
source share
5 answers

You need to use the delegate in the .h cell file. Declare a delegate as follows

@class MyCustomCell; @protocol MyCustomCellDelegate - (void) customCell:(MyCustomCell *)cell button1Pressed:(UIButton *)btn; @end 

then declare the field and property

 @interface MyCustomCell:UItableViewCell { id<MyCustomCellDelegate> delegate; } @property (nonatomic, assign) id<MyCustomCellDelegate> delegate; @end 

in .m file

 @synthesize delegate; 

and in the button method

 - (void) buttonPressed { if (delegate && [delegate respondToSelector:@selector(customCell: button1Pressed:)]) { [delegate customCell:self button1Pressed:button]; } } 

Your view controller should accept this protocol as follows

.h file

 #import "MyCustomCell.h" @interface MyViewController:UIViewController <MyCustomCellDelegate> ..... ..... @end 

in the .m file in cellForRow: you need to add the property delegate to the cell

 cell.delegate = self; 

and finally you implement the method from the protocol

 - (void) customCell:(MyCustomCell *)cell button1Pressed:(UIButton *)btn { } 

Sorry for my english and code. Wrote it from my PC without Xcode

+9
source

I had the same problem, and I solved it by subclassing the cell in my own class and putting the buttons in the form of outputs there and filling the cell with data from the model, using the method that returns the cell is currently being viewed.

For example, if you have a Person class, and each person has a first name, last name, and several friends. And every time you clicked a button in a cell, the number of friends for a particular person would increase by 1.

 _______________DATA SOURCE___________________________ #import <Foundation/Foundation.h> @interface Person : NSObject @property (nonatomic, strong) NSString *name; @property (nonatomic, strong) NSString *comment; @property (nonatomic) NSInteger numberOfFriends; +(instancetype)personWithName:(NSString *)aName Surname:(NSString *)aSurname; @end #import "Person.h" @implementation Person +(instancetype)personWithName:(NSString *)aName Surname:(NSString *)aSurname{ Person *person = [[Person alloc] init]; [person setName:aName]; [person setSurname:aSurname]; [person setNumberOfFriends:0]; return person; } @end _____________________PERSON CELL________________________ #import <UIKit/UIKit.h> @interface PersonCell : UITableViewCell @property (strong, nonatomic) IBOutlet UILabel *friendsNum; @property (strong, nonatomic) IBOutlet UIButton *friendsBtn; @property (strong, nonatomic) IBOutlet UILabel *nameLabel; @property (strong, nonatomic) IBOutlet UILabel *surnameLabel; @end 

Personally, I created a private NSArray to store the names of Person objects and a private NSMutableDictionary to store Person objects, and I set the keys as the names of people.

 _____________________PERSON TABLE VIEW________________________ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; PersonCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; NSString *name = [peopleNames objectAtIndex:indexPath.row]; Person *person = [people objectForKey:name]; if(cell == nil) { cell = [[PersonCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } // Configure the cell... cell.nameLabel.text = person.name; cell.surname.Label.text = person.surname [cell.friendsButton addTarget:self action:@selector(moreFriends:) forControlEvents:UIControlEventTouchUpInside]; cell.friendsNum.text = [NSString stringWithFormat:@"%i", person.numberOfFriends]; return cell; } - (IBAction)moreFriends:(id)sender { UIButton *btn = (UIButton *)sender; PersonCell *cell = [self parentCellForView:btn]; Person *person = [people objectForKey:cell.nameLabel.text]; person.numberOfFriends++; [self.tableView reloadData]; } -(PersonCell *)parentCellForView:(id)theView { id viewSuperView = [theView superview]; while (viewSuperView != nil) { if ([viewSuperView isKindOfClass:[PersonCell class]]) { return (PersonCell *)viewSuperView; } else { viewSuperView = [viewSuperView superview]; } } return nil; } 
+1
source

I would recommend using a delegate.

0
source

You might just want to add a selector for the button in your view controller, which has a table.

in your cellForIndexPath function

 [yourCell.button addTarget:self action:@selector(customActionPressed:) forControlEvents:UIControlEventTouchDown]; 

then handle the button click in the sender's customActionPressed: (id) method

  //Get the superview from this button which will be our cell UITableViewCell *owningCell = (UITableViewCell*)[sender superview]; //From the cell get its index path. NSIndexPath *pathToCell = [myTableView indexPathForCell:owningCell]; //Do something with our path 

This may be the best solution for you if there are no other factors that you have not indicated.

I have a tutorial that can explain more http://www.roostersoftstudios.com/2011/05/01/iphone-custom-button-within-a-uitableviewcell/

0
source

Why not create a delegate (using @protocol) for your custom cell. Then you can assign a main view, since each delegate is a cell and handle the action accordingly.

0
source

All Articles