0

I have a button (action) connected to my UITableViewCell class. How do I get indexPath of tableView from tableViewCell class?

@implement myTableViewCell

-(IBAction)buttonPressed{
    // Do something to get indexPath?
}
  • I wish to get indexPath from UITableViewCell class, not from UITableView class. – user3322987 Mar 16 '14 at 15:10
  • 1
    The most simple solution is to add a index property for your custom cell and keep updating it. – kukushi Mar 16 '14 at 15:25
  • 2
    I might be mistaken, but I think this answer http://stackoverflow.com/a/19441029/1187415 to the "possible duplicate" should work even from the table view cell. – Martin R Mar 16 '14 at 15:28
  • @MartinR I just saw it, I get error on the first line "UIView *parentCell = sender.superview;" The error message is Property 'superview' not found on object of type '__strong id'. What does it means? – user3322987 Mar 16 '14 at 15:47
  • @user3322987: Declaring the method as `-(void)button1Tapped:(UIButton *)sender` should solve that problem. – Martin R Mar 16 '14 at 15:50
  • (@user3322987: But check if a *cell* should really "know" its index path within the table view. Perhaps all tasks involving the index path are better delegated to the table view controller.) – Martin R Mar 16 '14 at 15:54
  • @MartinR all solutions that get indexPath from tableView class are directing my question toward "duplicate of other question". Just want to figure out a way to get parent's tableView indexPath from cell class. – user3322987 Mar 16 '14 at 16:00
  • I agree it shouldnt know its path, but I provided you the code. – Daij-Djan Mar 16 '14 at 16:01

4 Answers4

2

in CustomCell.h

@property (nonatomic, strong) UIButton *btn;

in tableView dataSource file

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
         cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
         [cell.btn addTarget:self action:@selector(buttonPressed:event:) forControlEvents:UIControlEventTouchUpInside];
}

//button action
-(void)buttonPressed:(UIControl *)sender event:(id)event{
   UITouch *touch = [[event allTouches] anyObject];
   CGPoint touchPos = [touch locationInView:self.tableView];
   NSIndexPath *indexPath = [self.tableView  indexPathForRowAtPoint:touchPos];
   if(indexPath != nil){
       //to do with indexPath 
   }
}

//button action in cell class

- (void)buttonPressed:(UIButton *)btn event:(id)event{
    UIView *superView = [btn superview];
    while (superView && ![superView isKindOfClass:[UITableView class]]) {
        superView = [superView superview];
    }

    if ([superView isKindOfClass:[UITableView class]]) {
        UITableView *tableView = (UITableView *)superView;
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint touchPos = [touch locationInView:tableView];
        NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:touchPos];
        if(indexPath != nil){
            NSLog(@"tableView.row:%d", indexPath.row);
        }
    }
}
simalone
  • 2,768
  • 1
  • 15
  • 20
  • My IBAction is in the Cell class. Just want to figure out a way to get parent's tableView indexPath from cell class. – user3322987 Mar 16 '14 at 15:54
  • @user3322987 I have edited my code. This is work for me nicely. – simalone Mar 16 '14 at 16:00
  • cell is only a view which will be reused by tableView, it can't have any indexPath information when ignore tableView actually use it. – simalone Mar 16 '14 at 16:03
  • Your answer works well, but my question haven't solve yet. Anyway thanks! – user3322987 Mar 16 '14 at 16:13
  • If you really want to do this in cell class ,just use `[btn superview]` recursive to find the tableView class, then use my method to get the indexPath, I have edited my answer, hope to help you. – simalone Mar 16 '14 at 17:09
  • I've looked everywhere - tried every thing. This was the only solution that worked. Thanks a lot. +1.. (I'm not sure why the action in the custom cell class is needed.. It wasn't for me) – ICL1901 Mar 04 '15 at 22:33
0

Like in this answer:

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
Community
  • 1
  • 1
lootsch
  • 1,867
  • 13
  • 21
  • The IBAction is in the UITableViewCell.m , how do I point to parent tableView? – user3322987 Mar 16 '14 at 15:11
  • add a delegate protocol to your `TableViewCell`, wich should tell your tableView, that it was pushed. – lootsch Mar 16 '14 at 15:17
  • I mean what should I cast on "self.tableView" from the code below? CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView]; – user3322987 Mar 16 '14 at 15:23
  • do you only need to know the index path inside the `tableViewCell`? Then you could add a `indexPath` property your `tableViewCell` and set it from your `tableView`. Then you would know it, when the action is called. – lootsch Mar 16 '14 at 15:26
  • Just want to figure out a way to get parent's tableView indexPath from cell class itself. – user3322987 Mar 16 '14 at 15:55
0

Add action to your button with sender:

    [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

then:

- (void) buttonPressed:(id)sender{
    CGPoint hitPoint = [sender convertPoint:CGPointZero toView:tableView];
    NSIndexPath *hitIndex = [tableView indexPathForRowAtPoint:hitPoint];

}
Magyar Miklós
  • 4,182
  • 2
  • 24
  • 42
0
  1. get the table from the cell. place this inside the cell

    - (UITableView*)tableView {
        if (tableView == nil) {
            tableView = (UITableView*)self.superview;
            while (tableView && ![tableView isKindOfClass:[UITableView class]]) {
                tableView = (UITableView*)tableView.superview;
            }
        }
        return tableView;
    }
    
  2. inside your IBAction ask for the indexpath

    NSIndexPath *p = [self.tableView indexPathForCell:self];
    

TADA ;)

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135