How to identify the row index and section number of a clicked button in a custom cell inside tableview

1.1k views Asked by At
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *simpleTableIdentifier = @"CustomCell";

    cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner: self options: nil ];
        cell = [nib objectAtIndex:0];


    }
    //cell.lblName.text = [items objectAtIndex:indexPath.row];
    NSMutableDictionary *dic = [items objectAtIndex:indexPath.row];
    cell.imgface.image = [dic objectForKey:@"Image"];
    cell.imgface.layer.cornerRadius = cell.imgface.frame.size.width / 2;
    cell.imgface.layer.borderWidth = 3.0f;
    cell.imgface.layer.borderColor = [UIColor whiteColor].CGColor;
    cell.imgface.clipsToBounds = YES;
    cell.lblName.text = [dic objectForKey:@"Name"];
    [cell.btnPhone setTitle:[dic objectForKey:@"Phone"] forState:(UIControlStateNormal)];
    cell.btnPhone.tag = indexPath.row;
    cell.btnstar.tag = indexPath.row;
    [cell.btnPhone addTarget:self action:@selector(dial:) forControlEvents:(UIControlEventTouchUpInside)];
    [cell.btnstar  addTarget:self action:@selector(toggleimage:) forControlEvents:UIControlEventTouchUpInside];
    if (indexPath.section == 0)
    {
        star = [UIImage imageNamed:@"Star-Favorites.png"];
        [cell.btnstar setBackgroundImage:star forState:UIControlStateNormal];
    }
    else
    {
        star = [UIImage imageNamed:@"keditbookmarks.png"];
        [cell.btnstar setBackgroundImage:star forState:UIControlStateNormal];

    }

    return cell;
}
3

There are 3 answers

0
johny kumar On BEST ANSWER
write below code in button IBAction and you will get indexpath.row and indexpath.section

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
NSLog(@"selected tableview row is %d",indexPath.row);
0
DeFrenZ On

You have some different ways to do that:
• When you configure the cell set the tag of the cell or the button as the row and retrieve that on the IBAction (I hate tags though)
• Define a delegate for the table cell and set it when you configure it. Then call the delegate method passing the cell and find the relative index (-indexPathForCell:)
• The same as before but pass the cell through a notification instead of delegates

0
Sheshnath On

first of all on, button IBAction call this method

- (IBAction)btnClick:(UIButton)sender 
{
   UITableViewCell *cellOfcontrol=[self cellWithSubview:sender];
   NSIndexPath *indexPath = [self.tableview indexPathForCell:cellOfcontrol];
}

cellWithSubview function

   - (UITableViewCell *)cellWithSubview:(UIView *)subview 
   {

    while (subview && ![subview isKindOfClass:[UITableViewCell self]])
    subview = subview.superview;
   return (UITableViewCell *)subview;
}