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 :
- 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 :
0
0
1
add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.
You can get the indexPath of the last row in last section like this. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(numberOfRowsInLastSection - 1) inSection:(numberOfSections - 1)]; Here, numberOfSections is the value you return from numberOfSectionsInTableView: method.
To create an IndexPath in objective C we can use. NSIndexPath *myIP = [NSIndexPath indexPathForRow: 5 inSection: 2] ; To create an IndexPath in Swift we can use.
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
With iOS7 you can get it like this :
- (IBAction)actionForTheUniqueButton:(id)sender
{
YouCellClass *clickedCell = (YouCellClass*)[[sender superview] superview];
NSIndexPath *indexPathCell = [self.tableView indexPathForCell:clickedCell];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With