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
Related
I have a product filter list and I would like to reset all data to default (without click) after clicked on Reset button.
But now my case is after I clicked on Reset button, selected items will not reset accordingly and it will change to other items. You may refer to screenshot as below:-
Here is my part of related code, please help and thanks for your advice:-
FilterItemCell.m
- (void)setUpUI
{
_contentButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_contentButton setTitleColor: ThemeBlueColor
forState:UIControlStateNormal];
[_contentButton addTarget:self action:#selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_contentButton];
}
- (void)checkButtonTapped:(id)sender
{
if ([sender isSelected]) {
!_attributeItemClick ? : _attributeItemClick();
[sender setSelected: NO];
[_contentButton setImage:nil forState:0];
[_contentButton setTitleColor:ThemeBlueColor forState:UIControlStateNormal];
} else {
!_attributeItemClick ? : _attributeItemClick();
[sender setSelected: YES];
[_contentButton setImage:[UIImage imageNamed:#"isSelectYes"] forState:0];
[_contentButton setTitleColor:ThemeRedColor forState:UIControlStateNormal];
}
}
Filter_ViewController.m
- (void)setUpBottomButton
{
CGFloat buttonW = FiltrateViewScreenW/2;
CGFloat buttonH = 40;
CGFloat buttonY = ScreenH -10 - buttonH;
NSArray *titles = #[#"Reset",#"OK"];
for (NSInteger i = 0; i < titles.count; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:titles[i] forState:UIControlStateNormal];
button.tag = i;
CGFloat buttonX = i*buttonW;
button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
button.titleLabel.font = PFR16Font;
if (i == 0)
{
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"Button2-1"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(bottomButtonClick:) forControlEvents:UIControlEventTouchUpInside];
}
else if (i == 1)
{
[button setBackgroundImage:[UIImage imageNamed:#"Button1-1"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(bottomButtonClick:) forControlEvents:UIControlEventTouchUpInside];
}
else
{
}
[_filtrateConView addSubview:button];
}
}
- (void)bottomButtonClick:(UIButton *)button
{
if (button.tag == 0) //Reset Button
{
//CURRENT CODE TO RELOAD COLLECTION VIEW DATA
[self.collectionView reloadData];
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
FilterItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:FilterItemCellID forIndexPath:indexPath];
cell.attribute_name=[[self.filterItem[indexPath.section] valueForKey:#"attribute_name"]objectAtIndex:indexPath.row];
return cell;
}
for reset function you need to renew all the data as in starting like you might be storing the ticked ones in an array.
you need to refresh the array you are using to display in your collectionView.
Then you need to call reload on your collectionView.
In your function I can see only reload function call not any function for refreshing the data in the array you might be using to display in the collection view.
I hope this helps you. Do let me know in case you need anything else.
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];
}
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]
I've added a button to section header which shows a view on clicking it. My need is that I've to show an "Up-arrow" image while view is shown and "Down-arrow" image when view is hidden.
How can I achieve this? Please help me ...
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 316, 60)];
[btn setTag:section];
[aView addSubview:btn];
[btn addTarget:self action:#selector(sectionTapped:) forControlEvents:UIControlEventTouchDown];
btn.backgroundColor=[UIColor clearColor];
}
I will suggest that you can have 2 images - 1 for 'Up' and 1 for 'Down' arrow.
Set default image to the btn for state UIControlStateNormal and set the other image for state UIControlStateSelected.
Now in your sectionTapped: method just change the state of the button.
So when you show or hide your view you need to set the button selected YES/NO.
Hope this helps.
I had done this before in one of my app.
Take this code as Reference
1.Specific Method.
- (void)methodName:(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:#"down.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateHighlighted];
}
else
{
numb = [NSNumber numberWithBool:YES];
sender.titleLabel.text = #"0";
[sender setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateHighlighted];
}
}
2.Setting UIButton Programatically in UITabelview viewForHeaderInSection.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(240, 20, 30, 30)];
[button addTarget:self
action:#selector(methodName:)
forControlEvents:UIControlEventTouchDown];
button.tag = section;
if([[sectionsArray objectAtIndex:section] boolValue])
{
[button setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateHighlighted];
button.titleLabel.text = #"0";
}
else
{
[button setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateHighlighted];
button.titleLabel.text = #"1";
}
You can trying using BOOL to check if the button has been pressed or not and animate to rotate the image in your button to point up or down.
-(IBAction)dropdownBtnClicked:(id)sender
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
if (!isDropdownEnabled)
{
//BOOL - checking if the button has been pressed or not
isDropdownEnabled=TRUE;
drpDwnBtn.transform=CGAffineTransformMakeRotation(270/180*M_PI);
}
else
{
isDropdownEnabled=FALSE;
drpDwnBtn.transform=CGAffineTransformMakeRotation(0);
}
[UIView commitAnimations];
}
In my iOS application, I have to change image as per button click.
I have to perform togling for single UIButton which I have generated from loop for different frame
It is working fine if I perform action on single button, if I have performing action for multiple button the image is not properly changing as per button action
Here is my button
btnFullScreen = [[UIButton alloc] init];
[btnFullScreen setBackgroundImage:[UIImage imageNamed:#"open icon.png"] forState:UIControlStateNormal];
[btnFullScreen setFrame:CGRectMake(self.view.frame.size.width-30,8,16,14)];
[btnFullScreen setBackgroundImage:[UIImage imageNamed:#"close icon.png"] forState:UIControlStateSelected];
[btnFullScreen addTarget:self action:#selector(showPopUp:) forControlEvents:UIControlEventTouchUpInside];
btnFullScreen.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleBottomMargin;
[self.view addSubview:btnFullScreen];
Here is my button action code
-(void)showPopUp:(id)sender{
if ([sender isSelected]) {
[sender setImage:[UIImage imageNamed:#"open icon.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
}else {
[sender setImage:[UIImage imageNamed:#"close icon.png"] forState:UIControlStateSelected];
[sender setSelected:YES];
}
[self.parent.parent addPopOversExcept:self.parent];
}
Thanks
I had done this before in one of my app.
I was creating button in each tableview cell.
So,Take this code as Reference.
1.Specific Method.
- (void)methodName:(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:#"down.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateHighlighted];
}
else
{
numb = [NSNumber numberWithBool:YES];
sender.titleLabel.text = #"0";
[sender setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateHighlighted];
}
}
2.Setting UIButton Programatically in UITabelview viewForHeaderInSection.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(240, 20, 30, 30)];
[button addTarget:self
action:#selector(methodName:)
forControlEvents:UIControlEventTouchDown];
button.tag = section;
if([[sectionsArray objectAtIndex:section] boolValue])
{
[button setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateHighlighted];
button.titleLabel.text = #"0";
}
else
{
[button setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateHighlighted];
button.titleLabel.text = #"1";
}