Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the indexPath of a UIButton in a UITableViewCell? [duplicate]

I have a UITableview with multiple cells. On each cell I'm adding buttons dynamically according to some array count. So, when I click on a button I'm able to get the tag value of the button. But how to get the indexPath of that cell?

Here is my code in -cellForRowAtIndexPath:

   UIView *view=(UIView*)[cell.contentView viewWithTag:indexPath.row+444];

   UIImageView *img=(UIImageView*)[cell.contentView viewWithTag:indexPath.row+999];
   img.image=[UIImage imageNamed:@"BHCS_empty.png"];

   if(integer!=50)
   {
        NSInteger y_axis=0;
        NSArray *Arr=[tableSubCategoryArr objectAtIndex:indexPath.row];
        img.image=[UIImage imageNamed:@"BHCS_selected.png"];

        view.Frame= CGRectMake(0,50,281, integer-50);
        for (int i=0; i<[Arr count]; i++)
        {
            NSLog(@"arr %@",[Arr objectAtIndex:i]);
            UIButton *Btn=[UIButton buttonWithType: UIButtonTypeCustom];
            Btn.frame=CGRectMake(0, y_axis, 281, 44);
            [Btn setImage:[UIImage imageNamed:@"BHCS_panel.png"] forState:UIControlStateNormal];
            [Btn addTarget:self action:@selector(subCategoryBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
            [Btn setTag:i+100];
            [view addSubview:Btn];


            UILabel *nameLbl=[[UILabel alloc]initWithFrame:CGRectMake(20, y_axis,248, 44)];
            nameLbl.text = [[Arr objectAtIndex:i]objectForKey:@"SubCategoryName"];
            nameLbl.textColor = [UIColor whiteColor];
            nameLbl.backgroundColor=[UIColor clearColor];
            panelTableView.separatorColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"BHCS_panel_div1.png"]];
            nameLbl.font = [UIFont fontWithName:@"Helvetica" size:12.0f];
            [view addSubview:nameLbl];

            y_axis=y_axis+44+1.3f;
        }
    }
like image 210
user2230971 Avatar asked Apr 26 '13 05:04

user2230971


People also ask

Is it possible to add UItableview within a UITableViewCell?

Implementation of adding a table view inside the cell of UItableview aka, Nested Table View. Used the Power of Autosizing table view and delegate to achieve the expansion and collapse of the cell height.

What is IndexPath section?

Index paths describe an item's position inside a table view or collection view, storing both its section and its position inside that section. For example, the first row in a table would have section 0, row 0, whereas the eighth row in the fourth section would have section 3, row 7.

What does IndexPath row return?

So to start with the indexPath. row will be 0. Then it will be 1, then 2, then 3 and so on. You do this so that you can get the correct string from the array each time.


2 Answers

Updated answer

Use it like:

CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView]; 
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];
like image 141
Amit Avatar answered Sep 21 '22 20:09

Amit


I have tried maximum of given answers, but at the and I generally use to go for most Generalised and ideal way as follows:

 CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
 NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
like image 33
Mrug Avatar answered Sep 23 '22 20:09

Mrug