http://stackoverflow.com/questions/9005052/recipes-to-pass-data-from-uitableviewcell-to-uitableviewcontroller
Solution: Use custom delegate
UIViewController
MyViewController.h
1 2 3 |
@interface MyViewController : UITableViewController <MyTableViewCellDelegate> @end |
MyViewController.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
@implementation MyViewController // methods... - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *reuseIdentifier = @"MyCell"; MyTableViewCell *cell = (id)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; if (cell == nil) cell = [[[MyTableViewCell alloc] initWithMyArgument:someArgument reuseIdentifier:reuseIdentifier] autorelease]; [cell setDelegate:self]; // update the cell with your data return cell; } - (void)myDelegateMethodWithCell:(MyTableViewCell *)cell { NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; // update model } @end |
Custom table cell and Protocol
MyTableViewCell.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
@protocol MyTableViewCellDelegate; @interface MyTableViewCell : UITableViewCell @property (assign, nonatomic) id <MyTableViewCellDelegate> delegate; - (id)initWithMyArgument:(id)someArgument reuseIdentifier:(NSString *)reuseIdentifier; @end @protocol MyTableViewCellDelegate <NSObject> @optional - (void)myDelegateMethodWithCell:(MyTableViewCell *)cell; @end |
MyTableViewCell.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
@implementation MyTableViewCell @synthesize delegate = _delegate; - (id)initWithMyArgument:(id)someArgument reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]; if (self) { // custom setup } return self; } - (void)prepareForReuse { [super prepareForReuse]; self.delegate = nil; } - (void)someActionHappened { if ([self.delegate respondsToSelector:@selector(myDelegateMethodWithCell:)]) [self.delegate myDelegateMethodWithCell:self]; } @end |