Detecting which button is clicked in UITableview and changing button image - iphone

I'm using three tables in my view and seprating them each other using a tag. In my second table view i made a cutom uitableview and adding labels and buttons inside them .
this is the button which i wrote inside the cellforrowatindexpath
followingButton = [UIButton buttonWithType:UIButtonTypeCustom];
[followingButton addTarget:self action:#selector(followingButtonpressed:)forControlEvents:UIControlEventTouchUpInside];
[followingButton setImage:[UIImage imageNamed:#"following12.png"] forState:UIControlStateNormal];
following.tag=indexpath.row;
[followingButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
followingButton.frame = CGRectMake(220.0 ,20.0, 100, 40.0);
[cell.contentView addSubview:followingButton];
======
-(void)followingButtonpressed:(id)sender
{
UIView *contentView1 = (UIView *)[sender superview];
UITableViewCell *cell1= (UITableViewCell *)[contentView1 superview];
NSIndexPath *indexPath1 = [followingTable indexPathForCell:cell1];
[sender tag];
NSLog(#"sender tag --%d",[sender tag]);
// UIButton *button = (UIButton *)sender;
// UITableViewCell *cell = (UITableViewCell*)[button superview];
UIButton *tempButtom = (UIButton *)[cell1 viewWithTag:[sender tag]];
[tempButtom setImage:[UIImage imageNamed:#"following_off12.png"]
forState:UIControlStateNormal];
}
But its crashing and i was not able to change the image of selected button , please help

Use this to detect which button has been pressed
UIView *senderButton = (UIView*) sender;
NSIndexPath *indexPath = [(UITableView *)self.superview indexPathForCell: (UITableViewCell*)[[senderButton superview]superview]];

Give Tag to each UIButton and write following code for detect which button is pressed
- (void)buttonPressedAction:(id)sender
{
UIButton *button = (UIButton *)sender;
// Get the UITableViewCell which is the superview of the UITableViewCellContentView which is the superview of the UIButton
(UITableViewCell*)cell = [[button superview] superview];
int row = [myTable indexPathForCell:cell].row;
// Here **row** give you tad of specific UIButton that you tapped
if (row == yourTag )
{
// write code for action
}
}
Or you can set tag for cell each time, but not UIButton. Your action will look then:
- (void)buttonPressedAction:(id)sender
{
UIButton *button = (UIButton *)sender;
int row = [button superview].tag;
// Here **row** give you tad of specific UIButton that you tapped
if (row == yourTag )
{
// write code for action
}
}

Related

Get the controls (UIButtons) and their tags of a UITableViewCell in a separate method

I have a UITableViewCell in which I'm creating some buttons (which are acting like radiobuttons) and assigning tags to the buttons. But the tags are not static. They are dynamic. Now when a particular radiobutton is clicked I need to set that button with some image (radio_On.png) and remaining all buttons as (radio_Off.png). But to set the images I couldn't understand how to get the tag values of all buttons in that particular cell because they are not static.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
btTemp1 = [[UIButton alloc]initWithFrame:CGRectMake(10, lblQuestion.frame.origin.y+lblQuestion.frame.size.height+3,17, 17)];
[btTemp1 addTarget:self action:#selector(radioButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
btTemp1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[btTemp1 setImage:[UIImage imageNamed:#"radio_button_off.png"] forState:UIControlStateNormal];
[btTemp1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btTemp1.titleLabel.font =[UIFont systemFontOfSize:14.f];
btTemp1.tag=++Optionid;
[hlcell.contentView addSubview:btTemp1];
btTemp2 = [[UIButton alloc]initWithFrame:CGRectMake(10, lblOption1.frame.origin.y+lblOption1.frame.size.height,17, 17)];
[btTemp2 addTarget:self action:#selector(radioButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
btTemp2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[btTemp2 setImage:[UIImage imageNamed:#"radio_button_off.png"] forState:UIControlStateNormal];
[btTemp2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btTemp2.titleLabel.font =[UIFont systemFontOfSize:14.f];
btTemp2.tag=++Optionid;
[hlcell.contentView addSubview:btTemp2];
}
-(IBAction) radioButtonClicked:(UIButton *) sender {
UIButton *button = (UIButton *)sender;
NSLog(#"%d", [button tag]);
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)
[[sender superview] superview]];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
NSMutableArray *cellSection = [self.finalarray objectAtIndex:indexPath.section];
[sender setImage:[UIImage imageNamed:#"radio-On.png"] forState:UIControlStateNormal];
}
Here one radiobuttton is not getting released when I check other button in TableView cell.
EDIT:
Prevoiusly I gave this code:
-(IBAction) radioButtonClicked:(UIButton *) sender {
UIButton *button = (UIButton *)sender;
NSLog(#"%d", [button tag]);
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)
[[sender superview] superview]];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
NSMutableArray *cellSection = [self.finalarray objectAtIndex:indexPath.section];
UIButton *btnTemp1 = (UIButton*)[cell viewWithTag:10]; //Static .But now I want dynamic
UIButton *btnTemp2 = (UIButton*)[cell viewWithTag:11];//Static .But now I want dynamic
UIButton *btnTemp3 = (UIButton*)[cell viewWithTag:12];//Static .But now I want dynamic
UIButton *btnTemp4=(UIButton *)[cell viewWithTag:13];//Static .But now I want dynamic
UIButton *btnTemp5=(UIButton *)[cell viewWithTag:14];//Static .But now I want dynamic
[radioButtonsinaSection addObject:btnTemp1];
[radioButtonsinaSection addObject:btnTemp2];
[radioButtonsinaSection addObject:btnTemp3];
[radioButtonsinaSection addObject:btnTemp4];
[radioButtonsinaSection addObject:btnTemp5];
}
for(int i=0;i<[radioButtonsinaSection count];i++){
[[radioButtonsinaSection objectAtIndex:i] setImage:[UIImage imageNamed:#"radio_button_off.png"] forState:UIControlStateNormal];
}
[sender setImage:[UIImage imageNamed:#"radio-On.png"] forState:UIControlStateNormal];
}
How can I get the all the controls (buttons and their tags) of a cell in which button (radiobutton) is clicked ?
As per my knowledge you miss the loop in radioButtonClicked method,which keeps track of which button is clicked..you try this code..
(IBAction) radioButtonClicked:(UIButton *) sender{
for(int i=0;i&lt;[self.radioButtons count];i++){
[[self.radioButtons objectAtIndex:i] setImage:[UIImage
imageNamed:#"radio-off.png"]
forState:UIControlStateNormal];
}
[sender setImage:[UIImage imageNamed:#"radio-On.png"]
forState:UIControlStateNormal];
}
I think it helps you,if not feel free to ask again..
Enjoy..

Change Image of UIButton in UITableView Cell Dynamically

I add UIButton in the cell Dynamically
and change UIButton image on click of UIBarButtonItem
how i can change?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Display"] ;
[cell.textLabel setText:[NSString stringWithFormat:#"%#" ,[arrItemList objectAtIndex:indexPath.row]]];
UIButton *btnCheck = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnCheck.frame = CGRectMake(6.0f, 2.0f, 32.0f, 25.0f);
[btnCheck setTitle:#"" forState:UIControlStateNormal];
[btnCheck addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btnCheck];
return cell;
}
//Here IBAction for Bar Button Item
//when ever user click on BarButton then set the image of all btnCheck
-(IBAction)bbtnCheck:(id)sender
{
}
Assuming that your question is how to change the image of a button w/in every cell of your table when a bar button is clicked, I suggest the following:
Within tableView:cellForRowAtIndexPath, add:
btnCheck.tag = MY_BUTTON_TAG_NUMBER;
Then, when the bar button item is clicked, loop through the visible cells and use the button's tag to identify it and update its image.
-(IBAction)bbtnCheck:(id)sender {
for (UITableViewCell *cell in self.tableView.visibleCells) {
UIButton *button = [cell viewWithTag:MY_BUTTON_TAG_NUMBER];
[button setImage:[UIImage imageNamed:SOME_NEW_IMAGE] forState:UIControlStateNormal];
}
}
In that bbtnCheck you have to get the instance of that tableView (i.e. particular cell)..Then after you can get buttons added on that particular cell..
Please Check this....
-(IBAction)bbtnCheck:(id)sender
{
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:YourRow inSection:YourSection]; // you have to set Row and Section according to your requirement
UITableViewCell *cellNew = [self.tblComments cellForRowAtIndexPath:indexpath];
for (UIButton *btn in [cellNew.contentView subviews])
{
if ([btn isKindOfClass:[UIButton class]])
{
[btn setImage:[UIImage imageNamed:#"yourImage.png"] forState:UIControlStateNormal];
}
}
}

Adding Radio Button in Custom Cell of TableView and unable to access outside

I am adding Radio Buttons in custom cell of UITableView and calling a method on it to changes its image or background Image. I able to set Selected button Image from sender but I want to make other buttons to their original position which I am unable to set it, as below functionality need to be look like proper Radio buttion in a cell
I am also facing problem in fetching proper Index Path in this method - (void) TableRadioButtonSelection:(id)sender;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *RadioIdentifier = #"RadioCellIdentifier";
RadioBtnCell *myRadiocell = (RadioBtnCell *)[tableView dequeueReusableCellWithIdentifier:RadioIdentifier];
if(myRadiocell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"RadioBtnCell" owner:self options:nil];
myRadiocell = radioCell;
//[myRadiocell.radioBtn1 setBackgroundImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
myRadiocell.radioBtn1.tag = ButtonTag;
//[myRadiocell.radioBtn2 setBackgroundImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
myRadiocell.radioBtn2.tag = ButtonTag1;
//[myRadiocell.radioBtn3 setBackgroundImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
myRadiocell.radioBtn3.tag = ButtonTag2;
myRadiocell.tag = RadioTag;
[myRadiocell.radioBtn1 addTarget:self action:#selector(TableRadioButtonSelection:) forControlEvents:UIControlEventTouchUpInside];
[myRadiocell.radioBtn2 addTarget:self action:#selector(TableRadioButtonSelection:) forControlEvents:UIControlEventTouchUpInside];
[myRadiocell.radioBtn3 addTarget:self action:#selector(TableRadioButtonSelection:) forControlEvents:UIControlEventTouchUpInside];
myRadiocell.backgroundView.backgroundColor = [UIColor clearColor];
self.radioCell = nil;
}
return myRadiocell;
}
- (void) TableRadioButtonSelection:(id)sender {
//RadioBtnCell *buttonCell = (RadioBtnCell*)[[btn superview] superview];
RadioBtnCell *cell = (RadioBtnCell *)[self.questionTblView viewWithTag:RadioTag];
UIButton *mybtn = (UIButton *)[cell.contentView viewWithTag:ButtonTag];
UIButton *mybtn1 = (UIButton *)[cell.contentView viewWithTag:ButtonTag1];
UIButton *mybtn2 = (UIButton *)[cell.contentView viewWithTag:ButtonTag2];
[mybtn setBackgroundImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
[mybtn1 setBackgroundImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
[mybtn2 setBackgroundImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
//NSLog(#"%#",[cell subviews]);
UIView* mysubview = [cell.contentView viewWithTag:ButtonTag]; // Make sure your UITextField really *is* the last object you added.
UIView* mysubview1 = [cell.contentView viewWithTag:ButtonTag1];
UIView* mysubview2 = [cell.contentView viewWithTag:ButtonTag2];
// I am unable to set Image of other buttons apart from sender.
//for(UIView *item in [mysubview subviews]) {
if ([mysubview isKindOfClass:[UIButton class]]) {
UIButton *newBtn = (UIButton*)mysubview;
UIButton *newBtn1 = (UIButton*)mysubview1;
UIButton *newBtn2 = (UIButton*)mysubview2;
NSLog(#"OK %#",newBtn);
//[newBtn setBackgroundImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
[newBtn setImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
[newBtn1 setImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
[newBtn2 setImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
}
//}
NSIndexPath *indexPath = [self.questionTblView indexPathForCell:cell];
NSLog(#"%d",indexPath.row);
// Below code is setting Image of selected Button properly as checked but above code for making button onto original state is not setting button image or background image
UIButton *btn = (UIButton *)sender;
[btn setImage:[UIImage imageNamed:#"radiobtn_chk.png"] forState:UIControlStateNormal];
//[btn setBackgroundImage:[UIImage imageNamed:#"radiobtn_chk.png"] forState:UIControlStateNormal];
}
try changing this line to this;-
RadioBtnCell *cell = (RadioBtnCell *)[self.questionTblView viewWithTag:RadioTag];
to
RadioBtnCell *cell = (RadioBtnCell *)[self.questionTblView cellForRowAtIndexPath:*indexPathofthecell*];
In this way you can access the table view cell.After that you can get the button from cell content views and do further coding.
Suppose if you do not know the indexPath then you can create the indexPath fromm the rowNumber of the table view(I think you have stored the table row number in RadioTag, if yes so you can use this field as a parameter).

uitableview touch event

I created a button inside of UITableViewCell, and when I click a button ,I want go to author view control with index value.
tapped = [UIButton buttonWithType:UIButtonTypeCustom];
[tapped setFrame:CGRectMake(0, 0, 320, 100)];
[tapped setTitle:#"button" forState:UIControlStateNormal];
NSInteger tet;
tet = indexPath.row;
[tapped addTarget:self action:#selector(tapped:) forControlEvents: UIControlEventTouchDown];
[Cell addSubview:tapped];
You should tag a button with indexPath.row.
tapped.tag = indexPath.row;
Inside your event handling code, you should use that tag to find index.
-(void) tapped:(UIButton *)sender
{
UIButton *btn = (UIButton *)sender;
int index = btn.tag;
//Do rest...
}
you can tag the button as follows :
tapped.tag = indexPath.row
And in the the tapped method You can use it as follows:
-(IBAction)tapped:(id)sender
{
UIButton *btn = (UIButton *)sender;
int index = btn.tag;
}
use this index as per your requirement...
try this
-(void) tapped:(UIButton *)sender
{
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *indexPath = [myTableView indexPathForCell:clickedCell];
int section = indexPath.section;
// You get easily your index path row
int row = indexPath.row;
// push your controller
}
first update ur code
use ur event UIControlEventTouchUpInside
tapped = [UIButton buttonWithType:UIButtonTypeCustom];
[tapped setFrame:CGRectMake(0, 0, 320, 100)];
[tapped setTitle:#"button" forState:UIControlStateNormal];
NSInteger tet;
tet = indexPath.row;
[tapped addTarget:self action:#selector(tapped:) forControlEvents: UIControlEventTouchUpInside];
[Cell addSubview:tapped];

Add Radio Button in Tableview Got Some Problem

I know xcode don't have radio Button
so I try to add a custom button and make it action like a radio button
This is the image I use
and this is the code I set to cell
UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
[but setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[but setImage:[UIImage imageNamed:#"radio-on.png"] forState:UIControlStateSelected];
[but setFrame:CGRectMake(0, 0, 44, 44)];
[but addTarget:self action:#selector(radioButton:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView= but;
and this is the problem I want to ask is
how can I give a void in - (IBAction)radioButton:(UIButton *)button
To control Two Radio Button in Two Rows
If Row 1 Radio button's selected is YES
btn in row 2 will be btn.state=NO and won't response the
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
It will be like this pic
How to set the if conditions in - (IBAction)radioButton:(UIButton *)button
this pic is fake...I only add the button in the cell...and changed the text color
Great Thanks to all stack overflow friends~
OK..here is how I add a button into a tableview :
in tableviewController.h :
#interface RootViewController : UITableViewController {
NSMutableArray *radioButtonArray;
}
#property (nonatomic ,retain)NSMutableArray *radioButtonArray;
in tableviewController.h.m
- (void)viewDidAppear:(BOOL)animated {
radioButtonArray = [NSMutableArray new];
for (int i = 0; i < 30; i ++) {
UIButton *radioButton = [UIButton buttonWithType:UIButtonTypeCustom];
[radioButton setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[radioButton setImage:[UIImage imageNamed:#"radio-on.png"] forState:UIControlStateSelected];
[radioButton setFrame:CGRectMake(0, 0, 44, 44)];
[radioButton addTarget:self action:#selector(radioButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[radioButtonArray addObject:radioButton];
}
[super viewDidAppear:animated];
}
and give it a (IBAction) void
- (IBAction)radioButtonPressed:(UIButton *)button{
[button setSelected:YES];
// Unselect all others.
for (UIButton *other in radioButtonArray) {
if (other != button) {
other.selected=NO;
}
}
}
than you can add your button into the cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.accessoryView = [radioButtonArray objectAtIndex:[indexPath row]];
// Configure the cell.
return cell;
}
You will need an array of all the radio buttons. Remember that table cells get recycled/may not be visible, etc. so create an array just with the buttons and then grab the right button out of that array in your tableView:cellForIndexPath: method.
So in your tableView:cellForIndexPath: method you would do something like this:
cell.accessoryView = [myButtonArray objectAtIndex:[indexPath row]];
Then, in your radioButton: radioButtonPressed: method you would do:
// Select the pressed button.
[button setSelected:YES];
// Unselect all others.
for (UIButton *other in myButtonArray) {
if (other != button) {
[other setSelected:NO];
}
}