Change Image of UIButton in UITableView Cell Dynamically - iphone

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];
}
}
}

Related

Detecting which button is clicked in UITableview and changing button image

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
}
}

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).

how to handle toogle button on every and each on tableviewcell in iPhone

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.

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];
}
}

UITableViewCell and images overlapping

i have a strange issue with my UITableView...I add a UIButton as a subview to each cell , but when any of the cells gets out of view something happens, and when i scroll up again , i can see that the UIButton background images of some cells are overlapping !
The cellForRowAtIndexPath method i use contains the following code , which adds the UIButton instances
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:#"MyCell"];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
UIFont *titleFont = [UIFont fontWithName:#"Arial-BoldMT" size:14.0];
[[cell textLabel] setFont:titleFont];
[cell autorelease];
}
if([cell viewWithTag:indexPath.row]==nil){
UIButton *buttonLeft = [UIButton buttonWithType:UIButtonTypeCustom];
buttonLeft.tag=indexPath.row;
if ([[myArray objectAtIndex:indexPath.row] isEqualToString:#"item1"]) {
[buttonLeft addTarget:self action:#selector(doThat:) forControlEvents:UIControlEventTouchUpInside];
[buttonLeft setBackgroundImage:[UIImage imageNamed:#"item1.png"] forState:UIControlStateNormal];
[buttonLeft setFrame: CGRectMake( 5.0f, 6.2f, 30.0f, 30.0f)];
}else{
[buttonLeft addTarget:self action:#selector(doThat:) forControlEvents:UIControlEventTouchUpInside];
[buttonLeft setBackgroundImage:[UIImage imageNamed:#"item2.png"] forState:UIControlStateNormal];
[buttonLeft setFrame: CGRectMake( 5.0f, 6.2f, 30.0f, 30.0f)];
}
[cell addSubview:buttonLeft];
}
cell.textLabel.text=[NSString stringWithFormat:#" %#",[myArray objectAtIndex:indexPath.row]];
return cell;
}
Apparently , this code for some reason adds the UIButton everytime the cell is displayed. I want each button subview to be added only once..
What am i doing wrong ?
Your help is much appreciated
That is because the table view reuses cells for performance reasons. Therefore, the cellForRowAtIndexPath: method, correctly implemented like yours, returns cells that already exist and only creates a new one if there is no reusable cell available.
Put all your button-related code within if (cell == nil) {}, so that the button is only added to "fresh" cells and it should work!