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];
}
}
Related
What i am doing is , I have an UITableview and i added UIButton as a custom view . I am giving tag to each button and tag got received in the action method. When i press the button it change the images for selected and unselected button but when i scroll it , it will come to normal state.
This is my cell for row at index method
static NSString *CellIdentifier = #"Cell4";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [self tableviewCellWithReuseIdentifierFollowing:CellIdentifier];
}
followingButton = [UIButton buttonWithType:UIButtonTypeCustom];
[followingButton addTarget:self action:#selector(followingButtonpressed:)forControlEvents:UIControlEventTouchUpInside];
[followingButton setImage:[UIImage imageNamed:#"following12.png"] forState:UIControlStateNormal];
followingButton.frame = CGRectMake(220.0 ,20.0, 100, 40.0);
[cell.contentView addSubview:followingButton];
NSLog(#"row--%d",indexPath.row);
followingButton.tag=indexPath.row;
NSLog(#"followingButton.tag--%d",followingButton.tag);
[self configureCellFollowing:cell forIndexPath:indexPath];
return cell;
}
==================
//Here is the action method
-(void)followingButtonpressed:(id)sender
{
NSLog(#"sender tag --%d",[sender tag]);
UIButton *btnPly = (UIButton *)sender;
if([btnPly isSelected])
{
[btnPly setSelected:NO];
[btnPly setImage:[UIImage imageNamed:#"following12.png"] forState:UIControlStateNormal];
}
else
{
[btnPly setSelected:YES];
[btnPly setImage:[UIImage imageNamed:#"following_off12.png"] forState:UIControlStateNormal];
}
}
Note : This code create cell for each row of data (Not reuse cell)
You need to change only as describe , May be helpful for you
NSString *CellIdentifier = [NSString stringWithFormat:#"S%1dR%1d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
may be solve your problem :)
The reason your buttons are "resetting" is because tableView:cellForRowAtIndexPath gets called multiple times (whenever a cell is about to be visible).
Each time this gets called you are reinitializing your button and resetting the image to following12.png (the default state). That's why when you scroll the buttons appear to reset.
You can't rely on the cell itself to preserve state because the cell will get reset every time. You need to move your state somewhere else (like in an array instance variable). Then when you have to configure a new cell in tableView:cellForRowAtIndexPath you can initialize it to the proper state (based on the array).
So create an NSMutableArray instance variable called myStateArray (or whatever) to store your button states, then your cellForRowAtIndexPath should look more like:
static NSString *CellIdentifier = #"Cell4";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [self tableviewCellWithReuseIdentifierFollowing:CellIdentifier];
}
followingButton = [UIButton buttonWithType:UIButtonTypeCustom];
[followingButton addTarget:self action:#selector(followingButtonpressed:)forControlEvents:UIControlEventTouchUpInside];
// -- Fetch the state from your array
BOOL buttonPressed = [[self.myStateArray objectAtIndex:indexPath.row] boolValue];
// -- Initialize the button state correctly
[followingButton setSelected:buttonPressed];
if (buttonPressed) {
[followingButton setImage:[UIImage imageNamed:#"following12.png"] forState:UIControlStateNormal];
} else {
[followingButton setImage:[UIImage imageNamed:#"following_off12.png"] forState:UIControlStateNormal];
}
followingButton.frame = CGRectMake(220.0 ,20.0, 100, 40.0);
[cell.contentView addSubview:followingButton];
NSLog(#"row--%d",indexPath.row);
followingButton.tag=indexPath.row;
NSLog(#"followingButton.tag--%d",followingButton.tag);
[self configureCellFollowing:cell forIndexPath:indexPath];
return cell;
}
Then in your button press make sure to save the state:
-(void)followingButtonpressed:(id)sender
{
// -- Save the state
[self.myStateArray insertObject:[NSNumber numberWithBool:[btnPly isSelected]] atIndex:[sender tag]];
NSLog(#"sender tag --%d",[sender tag]);
UIButton *btnPly = (UIButton *)sender;
if([btnPly isSelected])
{
[btnPly setSelected:NO];
[btnPly setImage:[UIImage imageNamed:#"following12.png"] forState:UIControlStateNormal];
}
else
{
[btnPly setSelected:YES];
[btnPly setImage:[UIImage imageNamed:#"following_off12.png"] forState:UIControlStateNormal];
}
}
As I understand from code you create UIButton every time when UITableView asks for cell. In both cases if you create a new cell or you use dequeued one. Then you create and add one more button every time over already created one. Move button creation to cell creation if So your cellForRowAtIndexPath method should looks like
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell4";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [self tableviewCellWithReuseIdentifierFollowing:CellIdentifier];
followingButton = [UIButton buttonWithType:UIButtonTypeCustom];
[followingButton addTarget:self action:#selector(followingButtonpressed:)forControlEvents:UIControlEventTouchUpInside];
[followingButton setImage:[UIImage imageNamed:#"following12.png"] forState:UIControlStateNormal];
followingButton.frame = CGRectMake(220.0 ,20.0, 100, 40.0);
[cell.contentView addSubview:followingButton];
}
NSLog(#"row--%d",indexPath.row);
followingButton.tag=indexPath.row;
NSLog(#"followingButton.tag--%d",followingButton.tag);
[self configureCellFollowing:cell forIndexPath:indexPath];
return cell;
}
But this will not be competed solution of your problem. As you probably know UITableView uses "reusable" cells to decrease system memory usage. It uses only amount of cells as it necessary to show at the moment. So in table with 100 cells it will create actually about 10. And all of them wont store correct state of all amount pressed/unpressed buttons. To achieve correct behaviour you have to refuse using tags and use some model logic instead. Easiest way - NSMutableArray where you will store buttons states. In followingButtonpressed: method you will set proper object to YES/NO and in cellForRowAtIndexPath you will read this value.
Check code below
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell4";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [self tableviewCellWithReuseIdentifierFollowing:CellIdentifier];
followingButton = [UIButton buttonWithType:UIButtonTypeCustom];
[followingButton addTarget:self action:#selector(followingButtonpressed:)forControlEvents:UIControlEventTouchUpInside];
[followingButton setImage:[UIImage imageNamed:#"following12.png"] forState:UIControlStateNormal];
followingButton.frame = CGRectMake(220.0 ,20.0, 100, 40.0);
[cell.contentView addSubview:followingButton];
BOOL isSelected = [[statesArray objectAtIndex:btnPly.tag] boolValue];
[self setState:isSelected forButton:followingButton];
}
NSLog(#"row--%d",indexPath.row);
followingButton.tag=indexPath.row;
NSLog(#"followingButton.tag--%d",followingButton.tag);
[self configureCellFollowing:cell forIndexPath:indexPath];
return cell;
}
-(void)followingButtonpressed:(id)sender
{
NSLog(#"sender tag --%d",[sender tag]);
UIButton *btnPly = (UIButton *)sender;
BOOL isSelected = [[statesArray objectAtIndex:btnPly.tag] boolValue];
[statesArray replaceObjectAtIndex:btnPly.tag withObject:[NSNumber numberWithBool:!isSelected]];
if(isSelected)
{
[self setState:NO forButton:btnPly];
}
else
{
[self setState:YES forButton:btnPly];
}
}
- (void) setState:(BOOL)state forButton:(UIButton *)button
{
if(state)
{
[button setSelected:NO];
[button setImage:[UIImage imageNamed:#"following12.png"] forState:UIControlStateNormal];
}
else
{
[button setSelected:YES];
[button setImage:[UIImage imageNamed:#"following_off12.png"] forState:UIControlStateNormal];
}
}
Where statesArray is
NSMutableArray *statesArray = [NSMutableArray new];
and you have to create and initialize it somewhere in you class. Count of objects in statesArray has to be the same as count of cells int tableView.
This is because every time when you are scrolling then this code calling
[followingButton setImage:[UIImage imageNamed:#"following12.png"] forState:UIControlStateNormal];
so check now
static NSString *CellIdentifier = #"Cell4";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [self tableviewCellWithReuseIdentifierFollowing:CellIdentifier];
followingButton = [UIButton buttonWithType:UIButtonTypeCustom];
[followingButton addTarget:self action:#selector(followingButtonpressed:)forControlEvents:UIControlEventTouchUpInside];
[followingButton setImage:[UIImage imageNamed:#"following12.png"] forState:UIControlStateNormal];
followingButton.frame = CGRectMake(220.0 ,20.0, 100, 40.0);
[cell.contentView addSubview:followingButton];
[self configureCellFollowing:cell forIndexPath:indexPath];
}
return cell;
}
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];
}
}
}
I need to add a UIButton in my UITableView only with my first and last array count. I found that we can use tableFooterView, to add unbutton below our tableview. But how can I achieve this over my tableview and only with first and last array values? Here is my code,
- (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];
}
//Adding a UIButton in last row
NSInteger lastSectionIndex = [editTable numberOfSections] - 1;
NSLog(#"lastSectionIndex:%d",lastSectionIndex);
// Then grab the number of rows in the last section
NSInteger lastRowIndex = [editTable numberOfRowsInSection:lastSectionIndex] - 1;
NSLog(#"lastRowIndex:%d",lastRowIndex);
// Now just construct the index path
NSIndexPath *pathToLastRow = [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];
NSLog(#"last index:%#",pathToLastRow);
if (pathToLastRow.row == lastRowIndex)
{
NSLog(#"row enters");
checkButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[checkButton1 setFrame:CGRectMake(200, 0, 168, 168)];
[checkButton1 addTarget:self
action:#selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
[checkButton1 setBackgroundImage:[[UIImage imageNamed:#"Up Arrow.jpg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
editTable.tableFooterView = checkButton1;
[cell addSubview:checkButton1];
}
Now I receive the buttons in every cell of my tableview. How can I give the button only to my first and last row array values? Thanks in advance.
Change your if condition as,
if ((lastSectionIndex == indexPath.section && lastRowIndex == indexPath.row ) || (indexPath.section == 0 && indexPath.row == 0 ))
{
It will look like this,
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIButton *checkButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
checkButton1.tag = 100;//not recommended, I would suggest to use custom UITableViewCell class and add this button as subview inside its init method
[checkButton1 setFrame:CGRectMake(200, 0, 168, 168)];
[checkButton1 addTarget:self
action:#selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
[checkButton1 setBackgroundImage:[[UIImage imageNamed:#"Up Arrow.jpg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
[cell addSubview:checkButton1];
}
//Adding a UIButton in last row
NSInteger lastSectionIndex = [editTable numberOfSections] - 1;
NSLog(#"lastSectionIndex:%d",lastSectionIndex);
// Then grab the number of rows in the last section
NSInteger lastRowIndex = [editTable numberOfRowsInSection:lastSectionIndex] - 1;
NSLog(#"lastRowIndex:%d",lastRowIndex);
// Now just construct the index path
NSIndexPath *pathToLastRow = [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];
NSLog(#"last index:%#",pathToLastRow);
UIButton *checkButton1 = (UIButton *)[cell viewWithTag:100];//not recommended, I would suggest to use custom UITableViewCell class and add this button as subview inside its init method
if ((lastSectionIndex == indexPath.section && lastRowIndex == indexPath.row ) || (indexPath.section == 0 && indexPath.row == 0 ))
{
checkButton1.hidden = NO;
} else {
checkButton1.hidden = YES;
}
You dont have to declare checkButton1 in .h file. Make it a local variable as shown above. Then you can set hidden property to hide/show in difference cells. Instead of doing the above, you can also create this button in custom UITableViewCell class and set the hidden property as cell.checkButton1.hidden = YES. You need to subclass UITableViewCell for that.
instead of creating Button in CellforRow You can Create in ViewDidload And attach it with FooterView of table.
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[aButton setTitleColor:[UIColor colorWithWhite:0.0 alpha:0.56] forState:UIControlStateDisabled];
[aButton setBackgroundImage:[[UIImage imageNamed:#"test.png"] stretchableImageWithLeftCapWidth:kButtonSliceWidth topCapHeight:0] forState:UIControlStateNormal];
[aButton setTitle:#"Click me" forState:UIControlStateNormal];
[aButton.titleLabel setFont:[UIFont boldSystemFontOfSize:kFontSize14]];
[aButton setFrame:CGRectMake(10.0, 15.0, 300.0, 44.0)];
[self.tableView setTableFooterView:aButton];
Same thing you can do for headerview. or else You Can Use Viewforheader method
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 44)] autorelease]; // x,y,width,height
UIButton *reportButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
reportButton.frame = CGRectMake(80.0, 0, 160.0, 40.0); // x,y,width,height
[reportButton setTitle:#"rep" forState:UIControlStateNormal];
[reportButton addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchDown];
[headerView addSubview:reportButton];
return headerView;
}
You should add first button to your table view Header And other button should be added to your footer view Secion of tableView
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).
My code is working fine but it works only for a single cell. When i define 5 rows then it works only for the last cell. If I click on 1 cell then value displayimage on last cell only it not display where i am click and which cell i am click how to handle toogle button change image for each and every cell click on button image this code.
-(void)changeMapType:(id)sender
{
//changeimagetype =!changeimagetype;
// sender.selected = changeimagetype;
//sender.selected
changeimagetype =!changeimagetype;
if(changeimagetype == YES)
{
//flagButton=1;
//[check setImage:[UIImage imageNamed:#"alarm_OF.png"] forState:UIControlStateNormal];
onButtonView.image = [UIImage imageNamed:#"alarm_OF.png"];
[mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
}
else
{
//flagButton=2;
onButtonView.image = [UIImage imageNamed:#"alarm_ON.png"];
[mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
}
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 2;
}
// Customize the appearance of table view cells.
- (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];
mimageButton = [UIButton buttonWithType:UIButtonTypeCustom];
mimageButton.frame=CGRectMake(10, 10, 20, 20);
mimageButton.tag = 1;
//mimageButton = [UIButton buttonWithType:UIButtonTypeCustom];
//[mimageButton setImage:[UIImage imageNamed:#"alarm_ON.png”] //forState:UIControlStateNormal];
//[mimageButton setImage:[UIImage imageNamed:#"alarm_OF.png”] //forState:UIControlStateSelected];
onButtonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
onButtonView.tag = 2;
onButtonView.image = [UIImage imageNamed:#"alarm_ON.png”];
[mimageButton setBackgroundImage:[onButtonView.image stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
[cell.contentView addSubview:mimageButton];
[mimageButton addTarget:self action:#selector(changeMapType:) forControlEvents: UIControlEventTouchUpInside];
}
Can you help me correcting the code?
Try giving the tag for the button as the row number of the table. Then check the tag in the button press method and then perform the logic to change image.
-(void)changeMapType:(id)sender
{
UIButton *button = (UIButton *)sender;
// Check tag of button
// Some code
//Change image after checking
[button setImage:yourImage forState:UIControlStateNormal];
}
Take an Array, Store the selected buttons tag in the array and while displaying the button in the cellForRowAtIndex method. check out whether the buttons tag is in that array or not. If its there then show your selected button image else unselected buttons image.