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; }
hellosunschein
source share