Question

I have a view which contain a UITableView. The cells of this UITableView are created in Interface Builder, in order to have different kinds of cells. So the action of each button is managed in the cell classes like following.

- My UITableView containing different kinds of cells :

enter image description here

- The header file of my class for type one cells ("CellTypeOne.h") :

@interface CellTypeOne : UITableViewCell
{
}

- (IBAction)actionForFirstButton:(id)sender;
- (IBAction)actionForSecondButton:(id)sender;
- (IBAction)actionForThirdButton:(id)sender;
- (IBAction)actionForFourthButton:(id)sender;

- The header file of my class for type two cells ("CellTypeTwo.h") :

@interface CellTypeTwo : UITableViewCell
{
}

- (IBAction)actionForTheUniqueButton:(id)sender;

- The view which contain my table view ("ViewContainingMyTableView.h") :

@interface ViewContainingMyTableView : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
    UITableView *myTBV;
}

@property (retain, nonatomic) IBOutlet UITableView *myTBV;

@end

Here the thing I want to do :

When I click on the first button in the first cell, for example, I want to be able to show the indexPath of the "current" cell.

For example, I want to have the following output when :

  • I click on the first button in the first cell : 0
  • I click on the third button in the first cell : 0
  • I click on the first and unique button in the second cell : 1
  • etc...
Was it helpful?

Solution

since u are using the custom cell i think u need to handle selection also, because u are touching the button inside the custom cell not the cell itself therefore tableview delegate methods are not fired, better as i said in ur custom cell put a delegate method for example in ur custom cells

in your CellTypeOne.h add this

//@class CellTypeOne; //if u want t pass cell to controller
@protocol TouchDelegateForCell1 <NSObject> //this delegate is fired each time you clicked the cell
 - (void)touchedTheCell:(UIButton *)button;
 //- (void) touchedTheCell:(CellTypeOne *)cell; //if u want t send entire cell this may give error add `@class CellTypeOne;` at the beginning  
@end

@interface CellTypeOne : UITableViewCell
{

}
@property(nonatomic, assign)id<TouchDelegateForCell1> delegate; //defining the delegate
- (IBAction)actionForFirstButton:(id)sender;
- (IBAction)actionForSecondButton:(id)sender;
- (IBAction)actionForThirdButton:(id)sender;
- (IBAction)actionForFourthButton:(id)sender;

in your CellTypeOne.m file

@synthesize delegate; //synthesize the delegate 

- (IBAction)actionForFirstButton:(UIButton *)sender 
{ 
   //add this condition to all the actions becz u need to get the index path of tapped cell contains the button
    if([self.delegate respondsToSelector:@selector(touchedTheCell:)])
     {
        [self.delegate touchedTheCell:sender];
        //or u can send the whole cell itself
        //for example for passing the cell itself
        //[self.delegate touchedTheCell:self]; //while at the defining the delegate u must change the sender type to - (void)touchedTheCell:(CellTypeOne *)myCell; if it shows any error  in the defining of the delegate add "@class CellTypeOne;" above the defying the delegate
    }
}

and in your ViewContainingMyTableView.h

 @interface ViewContainingMyTableView : UIViewController <UITableViewDelegate, UITableViewDataSource ,TouchDelegateForCell1> //confirms to custom delegate like table  delegates
 {
    UITableView *myTBV;
 }

 @property (retain, nonatomic) IBOutlet UITableView *myTBV;

 @end

and in the ViewContainingMyTableView.m file

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{  
   //during the creating the custom cell 
   CellTypeOne *cell1 = [self.aTableView dequeueReusableCellWithIdentifier:@"cell"];
   if(cell1 == nil)
   { 
     cell1 = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
   }
     cell.delegate = self; //should set the delegate to self otherwise delegate methods does not called this step is important
}

//now implement the delegate method , in this method u can get the indexpath of selected cell 

- (void)touchedTheCell:(UIButton *)button
 {
    NSIndexPath *indexPath = [self.aTableView indexPathForCell:(UITableViewCell *)button.superview];
    NSLog(@"%@",indexPath.description);

 }
 /* if u pass the cell itself then the delegate method would be like below
- (void)touchedTheCell:(CellTypeOne *)myCell
 {
    NSIndexPath *indexPath = [self.aTableView indexPathForCell:myCell];//directly get the cell's index path
    //now by using the tag or properties, whatever u can access the contents of the cell
    UIButton *myButton = [myCell.contentView viewWithTag:1000]; //get the button 
    //... u can access all the contents in cell 
 }
 */

in your case, this is for first button in the cell, add the delegate methods for each buttons having different functions, repeat above procedure for another buttons in the cell

hope u get this :) hapy coding

OTHER TIPS

With iOS7 you can get it like this :

- (IBAction)actionForTheUniqueButton:(id)sender
{
    YouCellClass *clickedCell = (YouCellClass*)[[sender superview] superview];
    NSIndexPath *indexPathCell = [self.tableView indexPathForCell:clickedCell];
}

You may use tag inside your xib on each cell to differentiate them.

Use after [self tag] inside the cell class

create tableView IBOutlet in .h or .m file. You can get the indexpath of selected tablecell by using inside ur actionForFirstButton: method

NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

 NSLog(@"I click on the first button in the first cell : %d", indexPath.row);

edited : use tag of the button to get the index path , in your actionForFirstButton method

if(sender.tag == 0 || 1 || 2 || 3){
     {
       NSLog(@"the cell is : %d", 0);
     }
    else if(sender.tag == 4){
    {
      NSLog(@"the cell is : %d", 1);
    }
    else{
    NSLog(@"the cell is : %d", 2);
    }

Your requirement is just to get back the corresponding row value of a button...

Then you have a very simple way ,

@interface CellTypeOne : UITableViewCell
{
    NSNumber *rowval;
}
@property (nonatomic,retain)NSNumber* rowval;

same thing in your CellTypeTwo.h

@interface CellTypeTwo : UITableViewCell
{
    NSNumber *rowval;
}
@property (nonatomic,retain)NSNumber* rowval;

and now in both .m file you must have defined a method for buttons, So with in those method just paste this line

NSLog(@"Number : %d",[rowval integerValue]);

and within )tableView cellForRowAtIndexPath: method add this line too

cell.rowval = [[NSNumber alloc]initWithInt:indexPath.row ];

Now every time you press a button of different cell it will reply back with row value of the cell

Hope this will help.. :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top