In my application I created a custom table view cell having a button inside it. I need to do specific actions when each button tapped. At the moment I could add actions to buttons, so that when I tapped, the actions are invoking but I cant identify which button is responsible for that action. How can I do different operations based on user event on buttons ? I could do this in normal way by setting "tag" property and checking using
[sender tag]
but dont know in this case. Thanks in advance.
Example:
[button1 addTarget: target action: #selector(action1) forControlEvents: UITouchUpInside];
[button2 addTarget: target action: #selector(action2) forControlEvents: UITouchUpInside];
[button3 addTarget: target action: #selector(action3) forControlEvents: UITouchUpInside];
Make a for loop and assign each button an incremental tag (based on row number possibly), as well as a single method in the view control that is the target (all buttons get the same selector).
In the called method, extract the tag number and perform the necessary action on it.
You're probably adding buttons to cells in cellForRowAtIndexPath.
If your table has only one section, then it's easy. Simply modify cellForRowAtIndexPath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// .. create cell
// .. add button
myButton.tag = indexPath.row;
[myButton addTarget: target action: #selector(buttonClicked:) forControlEvents: UITouchUpInside];
return cell;
}
-(void) buttonClicked:(id)sender {
NSLog (#"user clicked button in row %d", [(UIButton *)sender tag]);
// ..do your stuff here
}
Related
I have a created custom UIButton in UITableView viewForHeaderInSection. Now when i click on the button within the section how will i know which section is clicked? Please help.
when you add button in viewForHeaderInSection , just set tag with section number like bellow.,..
button.tag = section;
and when your button clicked at that time you can get that number with bellow code...
- (IBAction)yourButton_Clicked:(id) sender
{
NSLog(#"Section Number => %d",[sender tag]);
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIbutton *btn =[[UIButton alloc]init];
btn.tag=section;// this tag property will differentiate button for different section
}
by this you can access btn and add event on that burron
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIbutton *button =[[UIButton alloc]init];
button.tag=section;// this tag property will differentiate button for different section
}
In didselectrowatindexpath check for if(index path.section) is equal to the section you want the action.
I have a table where 5 cells are there. In each cell there is a button. I need to change those buttons dynamically on some conditions. How to change the button dynamically in table view.
What you can do is Write a function which will change the values of the buttons and then you can call that function. After changing button values then use any one of the mentioned method below:
You can use either : [tableview reloadData]; to reload all the table data.
OR
You can reload particular rows using the following method :
[tableview reloadRowsAtIndexPaths: indexPath withRowAnimation:anyKindOfAnimation];
You can query your table view for all visible cells through -visibleCells, get the reference of your button (assuming you have a UITableViewCell subclass with a property for the button) and change them.
First you need to set tags for each buttons. This can be done by
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Your code
cell.Button.tag = indexPath.row;
[cell.Button addTarget:self action:#selector(buttonClick1:) forControlEvents:UIControlEventTouchUpInside];
}
Then you can get the button from this method
- (IBAction)buttonClick1:(id)sender {
int tagNo =[sender tag];
UITableViewCell *cellclicked = [self.tblProfileFollowing cellForRowAtIndexPath:[NSIndexPath indexPathForRow:tagNo inSection:0]];
//Now change the action for the cell
[cellclicked.Button addTarget:self action:#selector(buttonClick2:) forControlEvents:UIControlEventTouchUpInside];
//Your Code
}
You can follow the same steps for - (IBAction)buttonClick2:(id)sender
Now you can track which button was clicked and change the method for that button.
And declare your cell and button strong.
I am trying to create a UITableView with a custom UIButton in each table cell. I implemented like this..
#implementation CouponDetailsCustomTableViewCell
...............
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
[self setBackgroundColor:[UIColor whiteColor]];
CGRect frame = self.contentView.frame;
self.radioButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.radioButton setImage:[UIImage imageNamed:#"radio_blank.png"] forState:UIControlStateNormal];
[self.radioButton setImage:[UIImage imageNamed:#"radio_selected"] forState:UIControlStateSelected];
[self.radioButton setFrame:CGRectMake(16, 10, 29, 29)];
[self.radioButton addTarget:nil action:#selector(radioButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:radioButton];
}
#end
and UITableView Delegate as......
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *COUPON_CELL_ID = #"CouponCell" ;
CouponDetailsCustomTableViewCell * couponCell = (CouponDetailsCustomTableViewCell *) [tableView dequeueReusableCellWithIdentifier:COUPON_CELL_ID];
if (couponCell == nil) {
couponCell = [[[CouponDetailsCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:COUPON_CELL_ID] autorelease];
couponCell.selectionStyle = UITableViewCellSelectionStyleNone;
}
[couponCell.radioButton setSelected:NO];
return couponCell;
}
and the radioButtonPressed method is
-(void)radioButtonPressed:(id) sender
{
[sender setSelected:YES];
}
Now i run the program and a custom UIButton is shown in every table row . If i click on a button it gets selected (shows the radio_selected.png).
The problem arises when i scroll down the table (i am showing only 4 rows of the table in the window) . When i scroll up again..what i see is the clicked button is showing the radio_blank.png.
I am new in iPhone development. i dont know why is this happening. The most i can guess is the button property is changing .. setSelected:NO.
Someone please give me suggestions to fix this problem.
Thank you.
When you scroll your UITableView, hidden cells are not rendered anymore and might be reused for cells that are becoming visible. If a new cell becomes visible, tableView:cellForRowAtIndexPath: gets called.
The problem is that you're setting the selected state there:
[couponCell.radioButton setSelected:NO];
Therefore, whenever you scroll your cell out of the visible area and back again, it gets reset to selected = NO.
I suggest you create a NSMutableDictionary where you store the selection state of each row/NSIndexPath, which you then re-apply in tableView:cellForRowAtIndexPath:.
replace [couponCell.radioButton setSelected:NO]; in tableView:cellForRowAtIndexPath: with code that sets the selected property depending on a state from your dataSource.
something along those lines:
/* get the button state from your data source */
FancyCouponObject *coupon = [self.coupons objectAtIndex:indexPath.row];
BOOL buttonState = coupon.buttonState;
[couponCell.radioButton setSelected:buttonState];
The cells of a tableView are reused when they are moved off screen. You can't save state in them.
problem is when you scroll the table at that time your cellForRowAtIndexPath: delegate method called for every row... so here when its called at time your setSelected Method call with NO argument like bellow...
[couponCell.radioButton setSelected:NO];
so when you scroll table at time your setSelected method call and your button turn with radio_blank.png
...
:)
-(void)radioButtonPressed:(id) sender
{
[sender setSelected:YES];
}
in this method you are setting button as selected and for selected button you have set the radio_blank.png image
I have a UITableViewCell with 3 checkmark buttons. Each checkmark button is a custom UIButton (just the images really), and have an outlet in the custom UITableViewCell subclass. In the cellForRowAtIndexPath method, I
[cell.firstcheckmark addTarget:self action:#selector(checkmarkPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell.secondcheckmark addTarget:self action:#selector(checkmarkPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell.thirdcheckmark addTarget:self action:#selector(checkmarkPressed:) forControlEvents:UIControlEventTouchUpInside];
- (void)checkmarkPressed:(id)sender {
// how do I get which checkmark was pressed?
}
I was not sure how to get which button was pressed from the sender object. Other times I can use the title of the button, but in this case, all the buttons are the same except their outlet. How do I get which button was pressed? Or do they each need to call a different method, and then in each of those three methods, I can update my model by calling the same modelUpdateMethod: from each of those three methods? Thanks.
You can identify them by their tag:
cell.firstcheckmark.tag = 0;
And then
- (void)checkmarkPressed:(id)sender {
UIButton *button = (UIButton*)sender;
if(button.tag == 0)
{
...
}
...
}
i think you can also use :
UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
it can get which cell the button in.
How do I add a custom button on a UITableViewCell, and then delete the cell with that button without using Interface Builder and Custom Cell?
If you really want to add a custom button WITHOUT subclassing, just add the button to the contentView of the cell:
[cell.contentView addSubview:customButton];
You can set all the characteristics of the button: frame, target, selector, etc... Ad then used the above call to add it to the cell.
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
customButton.frame=//whatever
[customButton setImage:anImage forState:UIControlStateNormal];
[customButton setImage:anotherImage forState:UIControlStateHighlighted];
[customButton addTarget:self action:#selector(delete) forControlEvents: UIControlEventTouchUpInside];
//yadda, yadda, .....
You can tag it as well
customButton.tag = 99999;
So you can find it later:
UIButton *abutton = (UIButton*) [cell.contentView viewWithTag:99999];
You will need to decide WHEN to add the button, maybe on cell selection, maybe in editing mode... just put the code in the delegate method of your choice.
If the button's sole purpose is to offer deletion you should look into UITableViewDataSource which has a method called - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath. Implement it like so:
- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
And then implement:
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Database removal code goes here...
}
To use these methods, let your UITableViewController implement the UITableViewDataSource protocol by doing something like:
MyClass : UITableViewController <UITableViewDataSource>
in your header file, and be sure to remember to set the viewController's datasource to self.