change the UIButton image in UITableView UIButton click - ios

In UITableView cellForRowAtIndexPath create one button with one image. write action for that button click to change the button images. here the code.
ButtonImage = [UIImage imageNamed:#"work_exe_old.png"];
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(250,10,34,34);
myButton.tag=22;
[myButton setBackgroundImage:ButtonImage forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(select_id:)forControlEvents:UIControlEventTouchDown];
- (IBAction)select_id:(id)search
{
UIButton *tempBtn =(UIButton *) search;
[tempBtn setImage:[UIImage imageNamed:#"work_exe.png"] forState:UIControlStateNormal];
}
clicked the button images changed(work_exe.png). wen click the same button again means the image want to change (work_exe_old.png) its like check box in tableview. how can i achieve tis

Instead of doing it in this way you can customise your code much better. Check the code below:
ButtonImage = [UIImage imageNamed:#"work_exe.png"];
ButtonImageSelected = [UIImage imageNamed:#"work_exe_old.png"];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(250,10,34,34);
myButton.tag=22;
[myButton setBackgroundImage:ButtonImage forState:UIControlStateNormal];
[myButton setBackgroundImage:ButtonImageSelected forState:UIControlStateSelected];
[myButton addTarget:self action:#selector(select_id:)forControlEvents:UIControlEventTouchDown];
And you can implement this selector method like this:
-(IBAction)select_id:(UIButton *) tempBtn {
[tempBtn setSelected:! tempBtn.selected];
}

set the uibutton's background images for each of its control states,
e.g.
UIImage *normal = [UIImage imageNamed:#"work_exe.png"];
UIImage *highlighted = [UIImage imageNamed:#"work_exe_old.png"];
UIImage *selected = [UIImage imageNamed:#"work_exe_old.png"];
[myButton setBackgroundImage:normal forState:UIControlStateNormal];
[myButton setBackgroundImage:highlighted forState:UIControlStateHighlighted];
[myButton setBackgroundImage:selected forState:UIControlStateSelected];

If you want do a action by clicking on UITableViewCell you have to implement the didSelectRowAtIndexPath and before decalre int i; gobally then follow the below code to make the changes for Button image
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
switch (i) {
case 0:
if(indexPath.row==0)
[tempBtn setImage:[UIImage imageNamed:#"work_exe.png"] forState:UIControlStateNormal];
}
else if (indexPath.row==1)
{
[tempBtn setImage:[UIImage imageNamed:#"work_exe.png"] forState:UIControlStateNormal];
}
else if (indexPath.row==2)
{
[tempBtn setImage:[UIImage imageNamed:#"work_exe.png"] forState:UIControlStateNormal];
}
.
.
.
else if (indexPath.row==n)
{
[tempBtn setImage:[UIImage imageNamed:#"work_exeN.png"] forState:UIControlStateNormal];
}
i=1;
break;
case 1:
if(indexPath.row==0)
[tempBtn setImage:[UIImage imageNamed:#"work_exe0.png"] forState:UIControlStateNormal];
}
else if (indexPath.row==1)
{
[tempBtn setImage:[UIImage imageNamed:#"work_exe1.png"] forState:UIControlStateNormal];
}
else if (indexPath.row==2)
{
[tempBtn setImage:[UIImage imageNamed:#"work_exe2.png"] forState:UIControlStateNormal];
}
.
.
.
else if (indexPath.row==n)
{
[tempBtn setImage:[UIImage imageNamed:#"work_exeN.png"] forState:UIControlStateNormal];
}
i=0;
break;
default:
break;
}
[yourtablename reloadData];
note: write this code after the cellForRowAtIndexPath method

I am not sure weather I understand you right. If you want the image to change after a click, you should save the current state of your image somewhere, e.g. in an NSInteger that holds the state for your cell as a number that represents which image should be shown. In the tableView:cellForRowAtIndexPath: method, put the right image (according to your NSInteger) into the cell.
#interface MyTabelViewClass()
#property (assign) NSInteger currentImageState;
...
#end
In the tableView:cellForRowAtIndexPath: method, you somewhere have to decide which image to chose, according to this property:
switch (self.currentImageState) {
case 0:myCell.imageView = [[UIImageView imageViewWithImage:[UIImage imageNamed:#"..."]]; break;
case 1:myCell.imageView = [[UIImageView imageViewWithImage:[UIImage imageNamed:#"..."]]; break;
...
}
In the tableView:didSelectRowAtIndexPath you have to change the state somewhere, like
self.currentImageState = (self.currentImageState + 1) % maximumNumberOfStates;
...
[self.myTableView reloadData]
...
Maybe it's a good idea to call reloadData with some delay because you want to wait for the deselection animation to finish first. You can do this by calling
[self.myTableView performSelector:#selector(reloadData) withObject:nil afterDelay:0.35]

Related

How to change Button image added to UITableview section header in ios

Here is my code
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Add_plus_iCon_iphone_4s"] forState:UIControlStateNormal];
button.frame = CGRectMake(sectionView.frame.size.width-10, 10, 20, 20);
[button addTarget:self action:#selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[sectionView addSubview:button];
}
- (void)btnAction:(UIButton *)sender
{
int i = [sender.titleLabel.text intValue];
NSNumber *numb;
if(i == 0)
{
numb = [NSNumber numberWithBool:NO];
sender.titleLabel.text = #"1";
[sender setImage:[UIImage imageNamed:#"Add_minus_iCon_iphone_4s"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"Add_plus_iCon_iphone_4s"] forState:UIControlStateHighlighted];
}
else
{
numb = [NSNumber numberWithBool:YES];
sender.titleLabel.text = #"0";
[sender setImage:[UIImage imageNamed:#"Add_plus_iCon_iphone_4s"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"Add_minus_iCon_iphone_4s"] forState:UIControlStateHighlighted];
}
}
First thing set images of button for both state (UIControlStateNormal, UIControlStateSelected) in ViewDidLoad and then do [cell.btnSelect setSelected:YES]; or [cell.btnSelect setSelected:NO]; as per requirement.
Otherwise change image for UIControlStateNormal when button tapped.
you are creating UIButton each time header load, which cause your button to reset,
1 way is to design cell in storyborad or xib and use reference,
Also have a DataSource to keep track of values
e.g.
if([dataSouce containsObject:yourItem]){
//selected button image
[cell.btnSelect setSelected:YES];
}
else{
//unselected button image
[cell.btnSelect setSelected:NO];
}

Set only single option in iOS

I have two custom round rect button and on this did tap event on image show.Like When we select gender there is only one option to select other automatically hide.but my gives when i touch first button than it select but when i touch second button no operation select.
My main moto is that i want to choose gender but in iOS no radio button use so i design custom and on click event set image.
calling api is a string type.
-(void)viewdidload
{
button = [UIButton buttonWithType:UIButtonTypeCustom];
callApi=#"MALE";//calling Api is a string type//
[button addTarget:self action:#selector(roundButtonDidTap:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)roundButtonDidTap:(UIButton*)tappedButton
{
if([callApi isEqualToString:#"FEMALE"]) {
UIImage *btnImage = [UIImage imageNamed:#"bullet.png"];
[button setImage:btnImage forState:UIControlStateNormal];
}
if ([callApi isEqualToString:#"MALE"]) {
UIImage *btnImage = [UIImage imageNamed:#"bullet.png"];
[button2 setImage:btnImage forState:UIControlStateSelected];
button.imageView.hidden=YES;
}
}
try this code:
-(IBAction)selectMale:(id) sender{ // MALE BUTTON
UIButton *RadioButton1 = (UIButton*)sender;
[self radiobuttonAction:RadioButton1];
}
-(void)radiobuttonAction:(UIButton *)Button
{
if(![Button isSelected])
{
[Button setSelected:YES];
[Button setImage:[UIImage imageNamed:#"radio_active.png"] forState:UIControlStateSelected];
gender = #"Male";
[btnFemale setImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateSelected];
[btnFemale setSelected:NO];
}
else
{
[Button setSelected:NO];
[Button setImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateNormal];
}
}
-(IBAction)selectFemale:(id) sender{
UIButton *RadioButton1 = (UIButton*)sender;
[self radiobuttonActionFemale:RadioButton1];
}
-(void)radiobuttonActionFemale:(UIButton *)Button
{
if(![Button isSelected])
{
[Button setSelected:YES];
[Button setImage:[UIImage imageNamed:#"radio_active.png"] forState:UIControlStateSelected];
gender = #"Female";
[btnMale setImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateSelected];
[btnMale setSelected:NO];
}
else
{

conditions checking for every single cell

I am having a table view, in the table cell I have two buttons and checking their conditions. its working but my problem is I want to check the button condition for every single cell in the table view. can anyone help me please.
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(80, 27, 36, 36);
[button1 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"l"ofType:#"png"]] forState:UIControlStateNormal];
button1.tag = indexPath.row;
[button1 addTarget:self action:#selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button1];
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.frame = CGRectMake(160, 27, 36, 36);
[button2 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"e"ofType:#"png"]] forState:UIControlStateNormal];
button2.tag = indexPath.row;
[button2 addTarget:self action:#selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button2];
- (void)radiobtn:(UIButton *)button
{
if(btn3 == 0) {
if ([button isSelected]) {
[button setImage:[UIImage imageNamed:#"l.png"]
forState:UIControlStateNormal];
[[NSUserDefaults standardUserDefaults]setBool:NO forKey:#"button1"];
[button setSelected:NO];
} else {
[button setImage:[UIImage imageNamed:#"lblue.png"]
forState:UIControlStateSelected];
[button setSelected:YES];
else{
}
}
- (void)radiobtn3:(UIButton *)button
{
if(btn1 == 0)
{
if ([button isSelected]) {
[button setImage:[UIImage imageNamed:#"v.png"]
forState:UIControlStateNormal];
[[NSUserDefaults standardUserDefaults]setBool:NO forKey:#"button3"];
}
[button setSelected:NO];
} else {
[button setImage:[UIImage imageNamed:#"vblue.png"]
forState:UIControlStateSelected];
[button setSelected:YES]; }}
else {
}
conditions are working good. but i want to check the conditions for every single cell . help me in coding.
You need to set tags for every button you allocate depending on your indexPath.row in cellForRowAtIndexPath datasource method of your UITableView. This link might help you out.
Maintain one array for checking the state of UIButton which will be class member.
Add #define for each button to know uniquely which button state we are referring like this :
#define kFirstBtnState #"firstbuttonstate"
#define kSecondBtnState #"secondbuttonstate"
Now initialize array in viewDidLoad like this :
//use tableview object array here
for(int i=0; i<[yourDataSourceOfTableViewArray count]; i++)
{
//Currently each button state is 0 which is false
NSDictionary *dictBtnState = [NSDictionary dictionaryWithObjectsAndKeys:#"0",kFirstBtnState,#"0",kSecondBtnState,nil];
[mutArrBtnStateMaintain addObject:dictBtnState];
}
Use created array in UITableView's cellForRowAtIndexPath like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
....................
....................
//get state list of buttons
NSDictionary *dictBtnState = [mutArrBtnStateMaintain objectAtIndex:indexPath.row];
BOOL firstBtnState = [[dictBtnState objectForKey:kFirstBtnState] boolValue];
BOOL secondBtnState = [[dictBtnState objectForKey:kSecondBtnState] boolValue];
//Act according to your requirement
return cell;
}
Note : Replace state in array when necessary to maintain the state of UIButton

UIButton in uiimageview in UITableview cell image not changing

I have a custom uitableviewcell in that i have an imageview with a button when user clicks the button, button should show with a tick mark!
When user clicks the button it should show with a tickmark
I have a tried some thing. pls help me
checkBoxButton.selected=!checkBoxButton.selected;
if(checkBoxButton.selected==1){
[checkBoxButton setBackgroundImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateSelected];
}
else{
[checkBoxButton setBackgroundImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
You can add properties which represent the status.
#property (nonatomic, assign) BOOL checked;
- (void)setChecked:(BOOL)checked
{
_checked = checked;
if (_checked) {
[self.checkboxButton setImage:[UIImage imageNamed:#"btn-checkbox-checked"] forState:UIControlStateNormal];
} else {
[self.checkboxButton setImage:[UIImage imageNamed:#"btn-checkbox-unchecked"] forState:UIControlStateNormal];
}
}
You may add target-action to your checkBoxButton. In the action method, set the value of checked property.
Try following code in your button click. This will work only if you have single
checkbox..
if ([self.checkBoxButton.currentTitle isEqualToString:#"uncheck"]) {
NSLog(#"changing to checked");
[self.checkBoxButton setTitle:#"check" forState:UIControlStateNormal];
[self.checkBoxButton setImage:[UIImage imageNamed:#"checkedbox.png"] forState:UIControlStateNormal];
}else if([self.checkBoxButton.currentTitle isEqualToString:#"check"])
{
NSLog(#"changing to UNchecked.....");
[self.checkBoxButton setTitle:#"uncheck" forState:UIControlStateNormal];
[self.checkBoxButton setImage:[UIImage imageNamed:#"uncheckedbox.png"] forState:UIControlStateNormal];
}
Hope this helps you...

Checkbox control for iOS application

Is there any checkbox control in the object library for iOS applications? The nearest thing I see is the switch control, which can take a boolean value.
You can use the selected state of a UIButton, set different images (or also texts) for differents (via code, or Interface Builder) :
[button setImage:[UIImage imageNamed:#"selected.png"]
forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:#"unselected.png"]
forState:UIControlStateNormal];
And in the touch up inside action :
- (IBAction)buttonTouched:(id)sender
{
button.selected = !button.selected;
// add other logic
}
//define property of button
Declare Unchecked Image
- (void)viewDidLoad
{
[_checkboxButton setBackgroundImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
Checked Image.
- (IBAction)buttonTapped:(id)sender
{
if (_checkboxButton.selected == YES)
{
[_checkboxButton setBackgroundImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateSelected];
_checkboxButton.selected = NO;
}
else
{
[_checkboxButton setBackgroundImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
_checkboxButton.selected = YES;
}
}
This is the code I use for two checkboxes on the screen. (I put them at the bottom of two pictures that are also on the screen. I use the UIControlEventTouchDown to call the checkBoxTapped method. This method talks to my main view controller where I decide if the answer was correct or incorrect. Then the main view controller calls back the this view and tells it to change the blank textbox to either a check:
or an x
// Put the Checkboxes on the screen
UIImage *checkBox = [UIImage imageNamed:#"Checkbox"];
self.checkBoxLeft = [UIButton buttonWithType:UIButtonTypeCustom];
[self.checkBoxLeft setImage:checkBox forState:UIControlStateNormal];
[self.checkBoxLeft setFrame:checkBoxFrameLeft];
[self.checkBoxLeft addTarget:self
action:#selector(checkBoxTapped:)
forControlEvents:UIControlEventTouchDown];
[self.checkBoxLeft setTitle:#"checkBoxLeft" forState:UIControlStateNormal];
self.checkBoxLeft.contentEdgeInsets = insets;
self.checkBoxLeft.showsTouchWhenHighlighted = NO; // Keeps it from turning gray
self.checkBoxRight = [UIButton buttonWithType:UIButtonTypeCustom];
[self.checkBoxRight setImage:checkBox forState:UIControlStateNormal];
[self.checkBoxRight setFrame:checkBoxFrameRight];
[self.checkBoxRight addTarget:self
action:#selector(checkBoxTapped:)
forControlEvents:UIControlEventTouchDown];
[self.checkBoxRight setTitle:#"checkBoxRight" forState:UIControlStateNormal];
self.checkBoxRight.contentEdgeInsets = insets;
self.checkBoxRight.showsTouchWhenHighlighted = NO; // Keeps it from turning gray
- (void)checkBoxTapped:(UIButton *)buttonPressed {
[[self delegate] checkBoxTapped:buttonPressed.currentTitle];
}
- (void)highlightCheckbox:(NSString *)checkboxToHighlight withHighlight:(NSString *)highlight {
if ( [checkboxToHighlight isEqualToString:#"left"] ){
[self.checkBoxLeft setImage:[UIImage imageNamed:highlight] forState:UIControlStateNormal];
} else {
[self.checkBoxRight setImage:[UIImage imageNamed:highlight] forState:UIControlStateNormal];
}
}

Resources