How to detect multiple buttons in tableview cell and my doubt is with example i have 3 buttons in cell if i tap on one button that button will change colour and and if i click indexpath.row=1 cell button that button will color also need to change help me
I did like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10.0, 0.0, self.tableView.frame.size.width/4, 40.0);
button.tag = 100 + indexPath.row*total_buttons_in_a_row;
[button setTitle:[NSString stringWithFormat:#"%ld",(long)button.tag] forState:UIControlStateNormal];
[button addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button2.frame = CGRectMake(10.0+self.tableView.frame.size.width/4+10.0, 0.0, self.tableView.frame.size.width/4, 40.0);
button2.tag = 100 + indexPath.row*total_buttons_in_a_row + 1;
[button2 setTitle:[NSString stringWithFormat:#"%ld",(long)button2.tag] forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button2];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button3.frame = CGRectMake(10.0+self.tableView.frame.size.width/4*2+10.0, 0.0, self.tableView.frame.size.width/4, 40.0);
button3.tag = 100 + indexPath.row*total_buttons_in_a_row + 2;
[button3 setTitle:[NSString stringWithFormat:#"%ld",(long)button3.tag] forState:UIControlStateNormal];
[button3 addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button3];
return cell;
}
-(void)btnClicked:(UIButton *)sender{
id selectedButton = [self.view viewWithTag:sender.tag];
if ([selectedButton backgroundColor] == [UIColor redColor]) {
[selectedButton setBackgroundColor:[UIColor clearColor]];
}else{
[selectedButton setBackgroundColor:[UIColor redColor]];
}
}
total_buttons_in_a_row is an Int. In your case define it in viewDidLoad total_buttons_in_a_row=3
P.S - set the Buttons CGRectMake according to your need.
Assign tag to each button using the combination of section & row..
When method assigned to button get called use '%' & '/' for further manipulation.
Let me know if you have any difficulty in implementing it..
Got to your CustomCell.h & right below #import add this code.
#protocol CustomCellDelegate <NSObject>
- (void)buttonActionwith :(NSIndexPath *)indexPath;
#end
Then Add this code right below #interface CustomCell : UITableViewCell
//Manual Properties
#property (strong, nonatomic) NSIndexPath *buttonIndexPath;
//Delegate
#property (nonatomic, weak) id <CustomCellDelegate> delegate;
The use of this NSIndexPath is to identify which cell user selecting.
Then go to CustomCell.m & in your button IBAction method add this code.
[self.delegate buttonActionwith:self.buttonIndexPath];
The meaning of this line of code is that, when the user TouchUpInside the Button in the cell buttonActionwith delegate method calls, if you set this CustomCellDelegate in your UIViewController where the UITableView is, that delegate method will call inside that ViewController too.
Now go to your MainViewController.h & add
CustomCellDelegate
& it will look like this after that,
#interface MainViewController : UIViewController <CustomCellDelegate,,UITableViewDataSource,UITableViewDelegate>
when creating the Custom UITableViewCell in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath don't forget to add this line of code before return cell;
//Set Cell Values Here
cell.delegate = self; //Setting delegate to self
cell.buttonIndexPath = indexPath;
The modified - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath will look like this,
static NSString *MyIdentifier = #"MyIdentifier"; //Set Identifier for cell
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: MyIdentifier]; //init CustomCell
if (cell == nil) { //Check cell is nill or not
NSArray *nib;
nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell"
owner:self options:nil]; //if cell is nil add CustomCell Xib
for (id oneObject in nib) if ([oneObject isKindOfClass:[CustomCell class]])
cell = (CustomCell *)oneObject;
}
//Set Cell Values Here
cell.buttonIndexPath = indexPath
return cell;
Finally add this method inside MainViewController.m
#pragma mark - SwipeableCellDelegate
- (void)buttonActionwith:(NSIndexPath *)indexPath { //Delegate method
NSLog(#"Button Clicks at index %ld",(long)indexPath.row);
}
This is how to add a single button inside the Cell. you can use this method to more buttons to the cell.
Related
I'm trying to get the indexPath.row of a button clicked inside a tableView row.
When the user clicks this button I get the index.row corresponding to the button very well, but when I add more objects to the source array to create more cells by calling reloadData, the rowButtonClicked in each cell it's no longer giving me the correct indexPath.row in example I press the index 20 and now the printed indexPath.row is 9.
In cellForRowAtIndexPath to add the event to the button:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
lBtnWithAction = [[UIButton alloc] initWithFrame:CGRectMake(liLight1Xcord + 23, 10, liLight1Width + 5, liLight1Height + 25)];
lBtnWithAction.tag = ROW_BUTTON_ACTION;
lBtnWithAction.titleLabel.font = luiFontCheckmark;
lBtnWithAction.tintColor = [UIColor blackColor];
lBtnWithAction.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[cell.contentView addSubview:lBtnWithAction];
}
else
{
lBtnWithAction = (UIButton *)[cell.contentView viewWithTag:ROW_BUTTON_ACTION];
}
//Set the tag
lBtnWithAction.tag = indexPath.row;
//Add the click event to the button inside a row
[lBtnWithAction addTarget:self action:#selector(rowButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
To do something with the clicked index:
-(void)rowButtonClicked:(UIButton*)sender
{
//Get the index of the clicked button
NSLog(#"%li", (long)sender.tag);
[self doSomething:(long)sender.tag];
}
Constants.h
#define ROW_BUTTON_ACTION 9
Why it is giving an incorrect index when I change the initial items of the tableView? Is there a way to solve this issue?
It looks like you're messing up button tags. Once you set the tag
lBtnWithAction.tag = indexPath.row;
you won't be able to get button correctly with
lBtnWithAction = (UIButton *)[cell.contentView viewWithTag:ROW_BUTTON_ACTION];
(assuming ROW_BUTTON_ACTION is a constant). lBtnWithAction will be nil all the time except when indexPath.row is equal to ROW_BUTTON_ACTION.
I would propose to subclass UITableViewCell, add a button-property there and then just refer to it directly instead of searching by tag. In this case you'll be able to use tags for buttons freely :) –
#interface UIMyTableViewCell : UITableViewCell
#property (nonatomic, strong, nonnull) UIButton *lBtnWithAction;
#end
And then in cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIMyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UIMyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell.lBtnWithAction addTarget:self action:#selector(rowButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
cell.lBtnWithAction.tag = indexPath.row;
return cell;
}
You can simply update you line -
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
to
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]
My take on this will be subclassing the UITableViewCell and providing a delegate for it to respond.
code:
//MyCoolTablewViewCell.h
#protocol MyCoolProtocol<NSObject>
-(void)youTouchedMyCoolCell:(MyCoolTableViewCell __nonnull *)myCoolCell;
#end
#interface MyCoolTableViewCell:UITableViewCell
#property(nonatomic, weak,nullable) id<MyCoolProtocol>myCooldelegate;
#end
//MyCoolTablewViewCell.m
#interface MyCoolTableViewCell(){
UIButton *mySuperCoolButton;
}
#end
#implementation MyCoolTablewViewCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if(!(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])){
return nil;
}
// init your stuff here
// init button
mySuperCoolButton = [UIBUtton buttonWithType:UIButtonTypeCustom];
[mySuperCoolButton addTarget:self action:#selector(tappedMyCoolButton:) forControlEvents:UIControlEventTouchUpInside];
return self;
}
- (void)tappedMyCoolButton:(id)sender{
if([_myCoolDelegate respondToSelector:#selector(youTouchedMyCoolCell:)]){
[_myCoolDelegate youTouchedMyCoolCell:self];
}
}
#end
then in your controller
// whatEverController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// get the cell
cell = [tableView dequeueReusableCellWithIdentifier:#"myCoolIdentifier"forIndexPath:indexPath];
cell.myCooldelegate = self;
}
-(void)youTouchedMyCoolCell:(MyCoolTableViewCell *)myCoolCell{
NSIndexPath *myCoolIndexPath = [_tableView indexPathForCell:myCoolCell];
// then do whatever you want with the index path
}
I have a custom UITableViewCell who contains 3 UIButtons and I want that when I tap one button, the background of my cell change. But when I tap a button all cells in my UITableView change their background and not only the cell who has the button inside it.
Custom Cell (.h)
#interface SCActionsViewCell : UITableViewCell
#property (nonatomic, weak) UITableView *tableView;
#property (nonatomic, strong) NSIndexPath *indexPath;
#property (nonatomic, strong) UIButton *thanksButton;
#property (nonatomic, strong) UIButton *commentsButton;
#property (nonatomic, strong) UIButton *reButton;
#end
Custom Cell (.m)
#define TEXT_BUTTON_SIZE 25
#implementation SCActionsViewCell
- (void)awakeFromNib {
// Initialization code
self.backgroundColor = [UIColor colorLight];
_thanksButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width/3, actionsCellHeight)];
_commentsButton = [[UIButton alloc] initWithFrame:CGRectMake(self.contentView.frame.size.width/3, 0, self.contentView.frame.size.width/3, actionsCellHeight)];
_reButton = [[UIButton alloc] initWithFrame:CGRectMake(self.contentView.frame.size.width*2/3, 0, self.contentView.frame.size.width/3, actionsCellHeight)];
_thanksButton.titleLabel.font = [UIFont fontWithName:kFontAwesomeFamilyName size:TEXT_BUTTON_SIZE];
_commentsButton.titleLabel.font = [UIFont fontWithName:kFontAwesomeFamilyName size:TEXT_BUTTON_SIZE];
_reButton.titleLabel.font = [UIFont fontWithName:kFontAwesomeFamilyName size:TEXT_BUTTON_SIZE];
[_thanksButton setTitle:[NSString fontAwesomeIconStringForEnum:FAThumbsOUp] forState:UIControlStateNormal];
[_commentsButton setTitle:[NSString fontAwesomeIconStringForEnum:FACommentsO] forState:UIControlStateNormal];
[_reButton setTitle:[NSString fontAwesomeIconStringForEnum:FARetweet] forState:UIControlStateNormal];
[_thanksButton setTitleColor:[UIColor sGreen] forState:UIControlStateSelected];
[_reButton setTitleColor:[UIColor sGreen] forState:UIControlStateSelected];
[self addSubview:_thanksButton];
[self addSubview:_commentsButton];
[self addSubview:_reButton];
}
#end
SCViewDataSource (TableviewDataSource)
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.posts.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SCActionsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"SCActionsViewCell" forIndexPath:indexPath];
cell.tableView = tableView;
cell.indexPath = indexPath;
[cell.thanksButton addTarget:(SCViewController *)tableView.delegate action:#selector(touched:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
SCViewController.m
-(void)touched:(UIButton *)button{
NSLog(#"indexPath : %#",((SCActionsViewCell *)button.superview).indexPath);
((SCActionsViewCell *)button.superview).backgroundColor = [UIColor blueColor];
}
In this case when I tap thanksButton in a cell all the cells change their background to blueColor...
(Try this) Updated you touched methood using following code.
-(void)touched:(UIButton *)button{
NSLog(#"indexPath : %#",((SCActionsViewCell *)button.superview).indexPath);
((SCActionsViewCell *)button.superview).contentView.backgroundColor = [UIColor blueColor];
}
Ok I found out by my own : don't use [tableView dequeueReusableCellWithIdentifier:forIndexPath:] but [[CustomCell alloc] init] and create your init function in your custom cell class.
Like this every cells will be a new object.
Add a property (maybe an NSDictionary or an NSArray) for tracking which indexPaths have been tapped already. Creating a new cell every time is very expensive. I suggest you don't abandon [tableView dequeueReusableCellWithIdentifier:forIndexPath:].
You could try this:
- (void)touched:(UIButton *)sender {
NSIndexPath *indexPath = ((SCActionsViewCell *)button.superview).indexPath;
[self.thankedIndexPaths addObject:indexPath];
}
Then you can set the background color accordingly:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SCActionsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"SCActionsViewCell" forIndexPath:indexPath];
cell.tableView = tableView;
cell.thanked = [self.thankedIndexPaths containsObject:indexPath];
cell.indexPath = indexPath;
[cell.thanksButton addTarget:(SCViewController *)tableView.delegate action:#selector(touched:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
I am trying to add buttons to my UITableView cells, using code like below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
UIButton *button = self.buttons[0];
[cell addSubview:button];
return cell;
}
The button isn't showing though. Is it possible to add buttons to a cell like this or do I have to make a custom UITableViewCell class?
When the button was made for the array the frame was set like below:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 70)]
The usage for adding elements have changed somewhen. Use
[cell.contentView addSubview:button];
and don't forget to proof before if a button is already added :P
enum
{
kButtonForA = 3333 // example
};
for every button set
button.tag = kButtonForA;
and use them
UIButton* btn = (UIButton *)[cell.contentView viewWithTag:kButtonForA];
if(!button)
btn = self.buttons[i];
UPDATE
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if(!self.buttons || [self.buttons count] == 0)
return 0;
return ...;
}
and after you init your array, use
[tableView reloadData];
just one possible way, but a fast one
try this in your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([cell.contentView viewWithTag:100] == nil) {
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(130,25, 40.0, 40.0);
[button setTitle:#"+" forState:UIControlStateNormal];
[button addTarget:self action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
button.tag=100;
[cell.contentView addSubview:button];
}
then perform action
-(void)aMethod:(id)sender {
NSInteger tag=[[sender superview] tag];
//NSInteger value=[[self.qtt objectAtIndex:tag] integerValue]+1;
//[self.qtt replaceObjectAtIndex:tag withObject:[#(value) stringValue]];
//[self.mytableview reloadData];
}
I have seen and checked multiple questions regarding adding an UIButton to a UITableViewCell but somehow I did not manage to find the exact problem that I am facing. When adding an UIButton to an UITableViewCell, the cell's text label overlaps with the button. The button is in the beginning of the row and it should be followed by the text label. I am most probably doing something wrong, but I can not find what. Here is my code for cellForRowAtIndex:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[self._tableView dequeueReusableCellWithIdentifier:#"event"];
Event* event =[self eventForIndexPath:indexPath];
if(cell==nil)
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"event"];
[[cell textLabel] setText:event.customer.name];
if(event.startTime!=nil)
[[cell detailTextLabel] setText:event.startTime];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
UIButton *cellButton =(UIButton*) [cell.contentView viewWithTag:CELL_BUTTON_TAG];
if(cellButton == nil){
UIButton *cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
cellButton.tag=CELL_BUTTON_TAG;
[cellButton setFrame:CGRectMake(0.0f, 0.0f, 44.0f, cell.frame.size.height)];
[cellButton setImage:[UIImage imageNamed:#"van_green.png"] forState:UIControlStateNormal];
[cellButton setImage:[UIImage imageNamed:#"location.png"]forState:UIControlStateSelected];
[cellButton addTarget:self action:#selector(carButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
cellButton.layer.borderWidth = 0.0;
[cell.contentView addSubview:cellButton];
}
return cell;
}
Sorry but your code is completely wrong..you are using an incorrect way and also you are adding a button each time.
The tableViewCell are REUSED so you have to add buttons and other UI elements just when you INIT the cell and not each time, or your app will finish in memory warning, crash, not fluid etc.
The correct way to do what you want to do is create your CustomTableViewCell subclassing the class UITableViewCell and add your labels and buttons. It is really simple:
1) Create a new file CustomSearchCell object that extends UITableViewCell:
CustomSearchCell.h
#import <UIKit/UIKit.h>
#interface CustomSearchCell : UITableViewCell
#property (strong, nonatomic) UIButton *button;
#property (strong, nonatomic) UILabel *lblDetails;
#end
CustomSearchCell.m
#import "CustomSearchCell.h"
#implementation CustomSearchCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
_lblDetails = [[UILabel alloc] initWithFrame:CGRectMake(100, 10, 200, 45)];
[_lblDetails setFont:[UIFont systemFontOfSize:20.0]];
[_lblDetails setTextColor:[UIColor blackColor]];
_button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, self.frame.size.height)];
[self.contentView addSubview: _lblDetails];
[self.contentView addSubview: _button];
}
return self;
}
#end
2) In your view controller:
#import "CustomSearchCell.h"
and:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ListCellIdentifier";
CustomSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[CustomSearchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//Settings
return cell;
}
Another your problem is when you press the button. You will receive the sender but the tableViewCell will can be used for a different indexPath when you intercept the touch..be careful..the correct way is another..you need to save in some way the actual indexPath when you press the button...
Problem is in this line [cell.contentView addSubview:cellButton];. When your cell going to be reuse, the cell already contains cell button, and you try to add that button again. Just do the below step to resolve this issue.
1) Add button tag to take from base view.
UIButton *cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
cellButton.tag = CELLBUTTONTAG;
2) Check condition view tag and add it to base view as below.
UIButton *cellButton = [cell.contentView viewWithTag:CELLBUTTONTAG];
if(cellButton == nil)
{
UIButton *cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
cellButton.tag = CELLBUTTONTAG;
...
[cell.contentView addSubview:cellButton];
}
You have given cellbutton's y position as 0.0f.That's y overlapping. try to change some value as you need.
[cellButton setFrame:CGRectMake(0.0f, 0.0f, 44.0f, cell.frame.size.height)];
to
[cellButton setFrame:CGRectMake(0.0f, some big value, 44.0f, cell.frame.size.height)];
I have a grouped UITableView (this one has 4 sections) and several rows each. I have programatically created 2 buttons in each of the cells.
This is how I am creating the UITableViewCell. In this code, I am trying to detect the indexPath.row and the indexPath.section of the button that was pressed and pass it to the method. How am I able to do this ?
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellId = #"Cell";
UITableViewCell *cell ;//= [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
NSString *docTitle = [currentDocument objectForKey:#"displayname"];
UIView *cellView = [[UIView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x+5, cell.contentView.frame.origin.y, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
UILabel *cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(cellView.frame.origin.x + 5, cellView.frame.origin.y + 5, cellView.frame.size.width - 10, 25)];
cellTitle.backgroundColor = [UIColor clearColor];
cellTitle.text = docTitle;
[cellView addSubview:cellTitle];
[cell.contentView addSubview:cellView];
UIButton *viewDocumentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[viewDocumentButton setTitle:#"View Online" forState:UIControlStateNormal];
viewDocumentButton.frame = CGRectMake(cellView.frame.origin.x + 5, cellTitle.frame.origin.y + cellTitle.frame.size.height + 5, 150, 35);
[viewDocumentButton addTarget:self action:#selector(openDocumentButtonPressedMethod:) forControlEvents:UIControlEventTouchDown];
[viewDocumentButton setTag:indexPath.row];
[cell.contentView addSubview:viewDocumentButton];
UIButton *downloadDocumentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[downloadDocumentButton setTitle:#"Download Document" forState:UIControlStateNormal];
downloadDocumentButton.frame = CGRectMake(cellView.frame.origin.x + 5, viewDocumentButton.frame.origin.y + viewDocumentButton.frame.size.height + 5, 150, 35);
[downloadDocumentButton addTarget:self action:#selector(openDocumentButtonPressedMethod:) forControlEvents:UIControlEventTouchDown];
[downloadDocumentButton setTag:indexPath.row];
[cell.contentView addSubview:downloadDocumentButton];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Additionally, you could...
- (IBAction)openDocumentButtonPressedMethod:(id)sender {
UIButton *button = (UIButton *)sender;
UIView *contentView = button.superview;
UITableViewCell *cell = (UITableViewCell *)contentView.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
// do something with the indexPath
}
And if you wanted to use the same method to handle both button events, then you could use the tag on the button to distinguish between the two buttons.
if (button.tag == 1) {
// do something with the indexPath view related
}
else {
// do something with the indexPath download related
}
Create a subclass of UIButton
#interface MyButton : UIButton
{ /* indexPath, other stuff */ }
#end
Then implement
- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
to do something with the indexPath and other stuff before invoking the super for this method.
When you create the button, provide indexPath.
In your openDocumentButtonPressedMethod: method, do
CGPoint pointInTableView = [self.tableView convertPoint:sender.bounds.origin fromView:sender];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:pointInTableView];