How to disable UIButton in tableview after called another UIVIew in ios - ios

I have 1 UITableView has multiple UIButton (named is "Open"button with different tags) in each row, i set tag for them. Now, after click on any button in each row, it'll show a UIView (detailView). On detailView has 1 "Submit"button. I want to when user click on "Submit" button, "Open"button with tag selected is disable. How can i do that? I used this code :
Code to create tableview with mutiple "Open"button:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"%d,%d",indexPath.section,indexPath.row];
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[market setTag:3000];
[market setFrame:CGRectMake(200, 6, 30, 30)];
[cell.contentView addSubview:market];
}
marketButton = (UIButton *)[cell.contentView viewWithTag:3000];
[marketButton setTag:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
_tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
return cell;
}
And code when click on"Open"button:
- (void)marketPressedAction:(id)sender
{
UIButton *button = (UIButton *)sender;
buttontag = button.tag;
NSLog(#"Market button click at row %d",buttontag);
if ([sender isSelected]) {
[sender setImage:[UIImage imageNamed:#"Marketplace.png"] forState:UIControlStateHighlighted];
[sender setSelected:NO];
}
else {
[sender setImage:[UIImage imageNamed:#"MarketplaceSelect.png"] forState:UIControlStateSelected];
[sender setSelected:YES];
}
}
}

If you want your "Open" button to be disabled after you click submit button, you should use delegates

UIButton *btn = (UIButton *)sender;
btn.enabled = NO;
Try this one in button click method

EDITED :
Change you marketPressedAction parameter id to UIButton * and write code of below
-(void)marketPressedAction:(UIButton *)sender
{
[sender setImage:[UIImage imageNamed:#"Marketplace.png"] forState:UIControlStateHighlighted];
[sender setImage:[UIImage imageNamed:#"MarketplaceSelect.png"] forState:UIControlStateSelected];
/// Here set you image ////////////////
[sender setImage:[UIImage imageNamed:#"myCustome.png"] forState:UIControlStateNormal];
[self createMarketPlaceForm]; // call detailView
sender.enabled = NO;
sender.userInteractionEnabled = NO;
}

Related

How to save the clicked button indexpath in custom cell?

Here is my problem, i have a custom cell in that 1 label and 4 buttons like question and answers,when the user click on the button 1 remaining 3 button go like this,
When the user scroll the cell it is not saving the what the user selected button i mean to say user click on the what ever the button it is not saving.
Here is my code,
ViewDidLoad()
{
testArray = [[NSMutableArray alloc]init];
for (int i =0; i<[mainArray count]; i++)
{
[testArray addObject:#"Unchecked"];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = #"Cell";
ContestQATableViewCell *cell =(ContestQATableViewCell *)[tableViewQA dequeueReusableCellWithIdentifier:cellId];
if (cell==nil)
{
NSArray *myNib;
myNib =[[NSBundle mainBundle]loadNibNamed:#"ContestQATableViewCell" owner:self options:nil];
cell = (ContestQATableViewCell *)[myNib lastObject];
}
cell.textLabel.text = [mainArray objectAtIndex:indexPath.row];
if([[testArray objectAtIndex:indexPath.row] isEqualToString:#"Unchecked"])
[cell.answer1 setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
else
[cell.answer1 setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
cell.answer1.tag = indexPath.row;
[cell.answer1 addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:cell.answer1];
return cell;
[cell.answer1 addTarget:self action:#selector(buttonsClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.answer2 addTarget:self action:#selector(buttonsClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.answer3 addTarget:self action:#selector(buttonsClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.answer4 addTarget:self action:#selector(buttonsClicked:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
and here is my add target method
-(void)buttonsClicked:(id)sender
{
UIButton *btn=(UIButton *)sender;
ContestQATableViewCell * cell=(ContestQATableViewCell *) [btn.superview superview];
if (cell.answer1.tag==btn.tag)
{
[cell.answer1 setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
}else{
[cell.answer1 setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
}
if (cell.answer2.tag==btn.tag)
{
[cell.answer2 setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
}else{
[cell.answer2 setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
}
if (cell.answer3.tag==btn.tag)
{
[cell.answer3 setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
}else{
[cell.answer3 setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
}
if (cell.answer4.tag==btn.tag)
{
[cell.answer4 setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
}else{
[cell.answer4 setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
}
}
For Check-Uncheck functionality only buttonClicked: method is not enough. You will have also put the condition in cellForRowAtIndexPath: method for which button is selected or which in unselected because cellForRowAtIndexPath: method will call each time when you will scroll your UITableView and cells will be refresh.
for example
I explain in step by step
Step-1
Create the two arrays one for gloabally another one for checking purpose
#interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
{
NSMutableArray *arrayforCheckUnchek; // handle which button is selected or which is unselected
NSMutableArray *originalArray; // original Array
}
Step-2
allocating the memory for arrays
- (void)viewDidLoad
{
[super viewDidLoad];
arrayforCheckUnchek = [[NSMutableArray alloc]init];
originalArray = [[NSMutableArray alloc]initWithObjects:#"cell1",#"cell2",#"cell3",#"cell4",#"cell5", nil];
// setting all cell initilayy at un check
for(int i=0; i<[originalArray count]; i++)
{
[arrayforCheckUnchek addObject:#"Unchecked"];
}
}
Step-2
setup your datasource Methods
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [originalArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *Identifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [originalArray objectAtIndex:indexPath.row];
UIButton *radiobutton = [UIButton buttonWithType:UIButtonTypeCustom];
[radiobutton setFrame:CGRectMake(270.0, 7.0, 30.0, 30.0)]; // customize the frames
if([[arrayforCheckUnchek objectAtIndex:indexPath.row] isEqualToString:#"Unchecked"])
[radiobutton setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
else
[radiobutton setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
radiobutton.tag = indexPath.row;
[radiobutton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:radiobutton];
return cell;
}
Step-3
here selected button index you can change the image
-(void)buttonClicked:(UIButton *)sender
{
//Getting the indexPath of cell of clicked button
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:yourtableviewname];
NSIndexPath *indexPath = [yourtableviewname indexPathForRowAtPoint:touchPoint];
//Checking the condition button is checked or unchecked.
if([[arrayforCheckUnchek objectAtIndex:indexPath.row] isEqualToString:#"Unchecked"])
{
[sender setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
[arrayforCheckUnchek replaceObjectAtIndex:indexPath.row withObject:#"Checked"];
}
else
{
[sender setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
[arrayforCheckUnchek replaceObjectAtIndex:indexPath.row withObject:#"Unchecked"];
}
}

Radio buttons get unselected while scrolling UITableView

I have two radio buttons on UITableView Cell.I am able to select one of them while scrolling UITableView down but when I scroll tableview up all radio buttons get unselected. After scrolling also I want to keep them selected but I am not able to do that....So please anyone having solution help me. Thank you
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cell";
customCell *cell = (customCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"customCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
leftBtnclick = [UIButton buttonWithType:UIButtonTypeCustom];
leftBtnclick.tag=999;
[leftBtnclick setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[leftBtnclick setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateSelected];
if (screenHeight == 667)
{
[leftBtnclick setFrame:CGRectMake(50, 59, 30, 30)];
}
else if(screenHeight == 480)
{
[leftBtnclick setFrame:CGRectMake(50, 40, 30, 30)];
}
[leftBtnclick addTarget:self action:#selector(leftTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.leftOptBtn addTarget:self action:#selector(leftTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:leftBtnclick];
rightBtnclick = [UIButton buttonWithType:UIButtonTypeCustom];
rightBtnclick.tag=1000;
[rightBtnclick setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[rightBtnclick setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateSelected];
if (screenHeight == 667)
{
[rightBtnclick setFrame:CGRectMake(180, 59, 30, 30)];
}
else if(screenHeight == 480)
{
[leftBtnclick setFrame:CGRectMake(50, 40, 30, 30)];
}
[rightBtnclick setFrame:CGRectMake(180, 59, 30, 30)];
[rightBtnclick addTarget:self action:#selector(rightTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.rightOptBtn addTarget:self action:#selector(rightTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:rightBtnclick];
cell.numberLbl.text = [numberArray objectAtIndex:indexPath.row];
cell.questionLbl.text = [questionArray objectAtIndex:indexPath.row];
[cell.leftOptBtn setTitle:[leftOptionArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
[cell.rightOptBtn setTitle:[rightOptionArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
return cell;
}
-(void)leftTickBtnClicked:(id)sender
{
UIButton *leftTickBtn=(UIButton *)sender;
leftTickBtn.selected=!leftTickBtn.selected;
for(UIView *vw in [[sender superview]subviews])
{
if([vw isKindOfClass:[UIButton class]] && vw.tag==1000)
{
UIButton *rightTickBtn=(UIButton *)vw;
if(leftTickBtn.selected)
{
rightTickBtn.selected=NO;
}
else
{
rightTickBtn.selected=YES;
}
}
}
}
-(void)rightTickBtnClicked:(id)sender
{
UIButton *rightTickBtn=(UIButton *)sender;
rightTickBtn.selected=!rightTickBtn.selected;
for(UIView *vw in [[sender superview]subviews])
{
if([vw isKindOfClass:[UIButton class]] && vw.tag==999)
{
UIButton *leftTickBtn=(UIButton *)vw;
if(rightTickBtn.selected)
{
leftTickBtn.selected=NO;
}
else
{
leftTickBtn.selected=YES;
}
}
}
}
You have created only one leftTickBtn and rightTickBtn and updated it each time when row is created. So finally whatever you have done with radio button, its affected to only last button.
Update your code as below :
UIButton *leftBtnclick = [UIButton buttonWithType:UIButtonTypeCustom];
leftBtnclick.tag=999+indexPath.row;
UIButton *rightBtnclick = [UIButton buttonWithType:UIButtonTypeCustom];
rightBtnclick.tag=1000+indexPath.row;
And implement methods like :
- (IBAction)leftTickBtnClicked:(UIButton *)sender
{
sender.selected = TRUE;
UITableViewCell *cell = [tblAppointment cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1000-sender.tag inSection:0]];
for (id view in cell.contentView.subviews)
{
if ([view isKindOfClass:[UIButton class]])
{
// your code
UIButton *btnRight = (UIButton *)view;
if (sender.selected == TRUE)
btnRight.selected = FALSE;
else
btnRight.selected = TRUE;
}
}
}
And
- (IBAction)rightTickBtnClicked:(UIButton *)sender
{
sender.selected = TRUE;
UITableViewCell *cell = [tblAppointment cellForRowAtIndexPath:[NSIndexPath indexPathForRow:999-sender.tag inSection:0]];
for (id view in cell.contentView.subviews)
{
if ([view isKindOfClass:[UIButton class]])
{
// your code
UIButton *btnLeft = (UIButton *)view;
if (sender.selected == TRUE)
btnLeft.selected = FALSE;
else
btnLeft.selected = TRUE;
}
}
}
You have this issue because of how the reusable cell works.
When you scroll away from the cell you made changes on the radio button , and then try to scroll back to that specific row ,a reusable cell dequeueReusableCellWithIdentifier does not guarantee that it will dequeue the exact same cell you made the changes and most likely it will dequeue a cell that you have not made any changes
You will need to keep track of the buttons selections in a List outside cellForRowAtIndexPath and reassign the selections to your radio buttons in cellForRowAtIndexPath much like the same idea with assigning the cell text every time with cell.numberLbl.text = [numberArray objectAtIndex:indexPath.row]; by using the indexPath.row in an array
See modification [1] , [2] and [3] on the code below (haven't tested it but you get the idea)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cell";
customCell *cell = (customCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"customCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
leftBtnclick = [UIButton buttonWithType:UIButtonTypeCustom];
leftBtnclick.tag=999;
[leftBtnclick setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[leftBtnclick setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateSelected];
if (screenHeight == 667)
{
[leftBtnclick setFrame:CGRectMake(50, 59, 30, 30)];
}
else if(screenHeight == 480)
{
[leftBtnclick setFrame:CGRectMake(50, 40, 30, 30)];
}
[leftBtnclick addTarget:self action:#selector(leftTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.leftOptBtn addTarget:self action:#selector(leftTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:leftBtnclick];
rightBtnclick = [UIButton buttonWithType:UIButtonTypeCustom];
rightBtnclick.tag=1000;
[rightBtnclick setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[rightBtnclick setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateSelected];
if (screenHeight == 667)
{
[rightBtnclick setFrame:CGRectMake(180, 59, 30, 30)];
}
else if(screenHeight == 480)
{
[leftBtnclick setFrame:CGRectMake(50, 40, 30, 30)];
}
[rightBtnclick setFrame:CGRectMake(180, 59, 30, 30)];
[rightBtnclick addTarget:self action:#selector(rightTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.rightOptBtn addTarget:self action:#selector(rightTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:rightBtnclick];
cell.numberLbl.text = [numberArray objectAtIndex:indexPath.row];
cell.questionLbl.text = [questionArray objectAtIndex:indexPath.row];
[cell.leftOptBtn setTitle:[leftOptionArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
[cell.rightOptBtn setTitle:[rightOptionArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
//[1]Set values for left and right
rightTickBtn.selected = [RightTicksArray objectAtIndex:indexPath.row];
leftTickBtn.selected = [LeftTicksArray objectAtIndex:indexPath.row];
return cell;
}
-(void)leftTickBtnClicked:(id)sender
{
UIButton *leftTickBtn=(UIButton *)sender;
leftTickBtn.selected=!leftTickBtn.selected;
for(UIView *vw in [[sender superview]subviews])
{
if([vw isKindOfClass:[UIButton class]] && vw.tag==1000)
{
UIButton *rightTickBtn=(UIButton *)vw;
if(leftTickBtn.selected)
{
rightTickBtn.selected=NO;
}
else
{
rightTickBtn.selected=YES;
}
//[2]Update RightTickArray At Pos
[RightTickArray objectAtIndex:indexPath.row] = leftTickBtn.selected;
}
}
}
-(void)rightTickBtnClicked:(id)sender
{
UIButton *rightTickBtn=(UIButton *)sender;
rightTickBtn.selected=!rightTickBtn.selected;
for(UIView *vw in [[sender superview]subviews])
{
if([vw isKindOfClass:[UIButton class]] && vw.tag==999)
{
UIButton *leftTickBtn=(UIButton *)vw;
if(rightTickBtn.selected)
{
leftTickBtn.selected=NO;
}
else
{
leftTickBtn.selected=YES;
}
//[3]Update LeftTickArray At Pos
[LeftTicksArray objectAtIndex:indexPath.row] = leftTickBtn.selected;
}
}
}
I got solution for my question. UITableView cells are reusable when you scroll tableview cell gets reused that's why radio button selection disappears. So solution is store your selection in one array use that array in cellForRowAtIndexPath. See code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cell";
customCell *cell = (customCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"customCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
leftBtnclick = [UIButton buttonWithType:UIButtonTypeCustom];
leftBtnclick.tag=999;
cell.leftOptBtn.tag=999;
[leftBtnclick setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[leftBtnclick setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateSelected];
[leftBtnclick setFrame:CGRectMake(50, 59, 30, 30)];
[leftBtnclick addTarget:self action:#selector(leftTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.leftOptBtn addTarget:self action:#selector(leftBtnClicked) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:leftBtnclick];
rightBtnclick = [UIButton buttonWithType:UIButtonTypeCustom];
rightBtnclick.tag=1000;
cell.rightTickBtn.tag=1000;
[rightBtnclick setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[rightBtnclick setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateSelected];
[rightBtnclick setFrame:CGRectMake(190, 59, 30, 30)];
[rightBtnclick addTarget:self action:#selector(rightTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.rightOptBtn addTarget:self action:#selector(rightBtnClicked) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:rightBtnclick];
cell.numberLbl.text = [numberArray objectAtIndex:indexPath.row];
cell.questionLbl.text = [questionArray objectAtIndex:indexPath.row];
[cell.leftOptBtn setTitle:[leftOptionArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
[cell.rightOptBtn setTitle:[rightOptionArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
if ([[answerArray objectAtIndex:indexPath.row] isEqualToString:#"YES"])
{
leftBtnclick.selected=YES;
rightBtnclick.selected=NO;
}
else if ([[answerArray objectAtIndex:indexPath.row] isEqualToString:#"NO"])
{
leftBtnclick.selected=NO;
rightBtnclick.selected=YES;
}
else
{
leftBtnclick.selected=NO;
rightBtnclick.selected=NO;
}
return cell;
}
-(void)leftTickBtnClicked:(id)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:questionTable];
NSIndexPath *indexPath1 = [questionTable indexPathForRowAtPoint:buttonPosition];
[answerArray replaceObjectAtIndex:indexPath1.row withObject:#"YES"];
UIButton *leftTickBtn=(UIButton *)sender;
leftTickBtn.selected=!leftTickBtn.selected;
for(UIView *vw in [[sender superview]subviews])
{
if([vw isKindOfClass:[UIButton class]] && vw.tag==1000)
{
UIButton *rightTickBtn=(UIButton *)vw;
if(leftTickBtn.selected)
{
rightTickBtn.selected=NO;
}
else
{
rightTickBtn.selected=YES;
}
}
}
NSLog(#"Answer Array: %#",answerArray);
}
-(void)rightTickBtnClicked:(id)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:questionTable];
NSIndexPath *indexPath1 = [questionTable indexPathForRowAtPoint:buttonPosition];
[answerArray replaceObjectAtIndex:indexPath1.row withObject:#"NO"];
UIButton *rightTickBtn=(UIButton *)sender;
rightTickBtn.selected=!rightTickBtn.selected;
for(UIView *vw in [[sender superview]subviews])
{
if([vw isKindOfClass:[UIButton class]] && vw.tag==999)
{
UIButton *leftTickBtn=(UIButton *)vw;
if(rightTickBtn.selected)
{
leftTickBtn.selected=NO;
}
else
{
leftTickBtn.selected=YES;
}
}
}
NSLog(#"Answer Array: %#",answerArray);
}

Button on cell change images - logic issue

Following is a code fragment from cellForRowAtIndexPath. I have added 2 buttons on a Cell.
When the user clicks on Button A the image of Button A should be a.png. And Button B should change to default.png.
When the user clicks on Button B the image of Button B should be b.png. And Button A should change to default.png.
How can i do this ?
cellForRowAtIndexPath Method
[cell.aButton addTarget:self action:#selector(methodA:) forControlEvents:UIControlEventTouchUpInside];
[cell.bButton addTarget:self action:#selector(methodB:) forControlEvents:UIControlEventTouchUpInside];
- (void) methodA:(id) sender {
[((UIButton *)sender) setImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateSelected];
// How to change the image of Button B to default.png ??
}
- (void) methodB:(id) sender {
[((UIButton *)sender) setImage:[UIImage imageNamed:#"b.png"] forState:UIControlStateSelected];
// How to change the image of Button A to default.png ??
}
If you have set tag to your buttons like aButton has tag= 1, and bButton has tag= 2
- (void) methodB:(id) sender {
UIButton *bButton =((UIButton *)sender) ;
[bButton setImage:[UIImage imageNamed:#"b.png"] forState:UIControlStateSelected];
UIButton *aButton = (UIButton *)[bButton.superview viewWithTag:bButton.tag-1];
[aButton setImage:[UIImage imageNamed:#"default.png"] forState:UIControlStateSelected];
}
- (void) methodA:(id) sender {
UIButton *aButton =((UIButton *)sender) ;
[aButton setImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateSelected];
UIButton *bButton = (UIButton *)[bButton.superview viewWithTag:aButton.tag+1]; //updated here, instead of bButton, put aButton
[bButton setImage:[UIImage imageNamed:#"default.png"] forState:UIControlStateSelected];
}
Enjoy Coding !!
Try this,
- (void) methodA:(id) sender {
UIButton *buttonA = (UIButton *)sender;
CGPoint pointInTable = [field convertPoint:buttonA.bounds.origin toView:_tableView];
NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:pointInTable];
UITableViewCell *cell = [_tableView cellForRowAtIndexPath:nowIndex];
//now access the button and change button images
//cell.aButton setImage:
//cell.bButton setImage:
}
Do the same for button B action.
Note: I recommend using custom tableview cell and handling this button actions inside the custom cell.
For both the buttons, set a.png and default.png for UIControlStateNormal and UIControlStateSelected respectively
Now you can simply make the button selected and deselected accordingly and donot worry about the image.
Hope this image gives you another hint.
To handle your selection issue, the best way is to handle it in your tableViewCell class, where you can access both the buttons and write a delegate or callback if required.
Try this...
cellForRowAtIndexPath
[cell.aButton addTarget:self action:#selector(methodToggle:) forControlEvents:UIControlEventTouchUpInside];
[cell.bButton addTarget:self action:#selector(methodToggle:) forControlEvents:UIControlEventTouchUpInside];
[cell.aButton setImage:[UIImage imageNamed:#"default.png"] forState:UIControlStateNormal];
[cell.bButton setImage:[UIImage imageNamed:#"default.png"] forState:UIControlStateNormal];
[cell.aButton setImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateSelected];
[cell.bButton setImage:[UIImage imageNamed:#"b.png"] forState:UIControlStateSelected];
cell.aButton.tag == 111;
cell.aButton.tag == 222;
Just One method for both Buttons
- (void) methodToggle:(id) sender {
UIButton * btn = (UIButton *)sender;
if (btn.tag == 111)
{
UIButton * btnAnother = (UIButton *)[[btn superview] viewWithTag:222];
btnAnother.selected = NO;
}
if (btn.tag == 222)
{
UIButton * btnAnother = (UIButton *)[[btn superview] viewWithTag:111];
btnAnother.selected = NO;
}
btn.selected = YES;
}
Inside tableView:cellForRowAtIndexPath: add following code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
[cell.aButton setTag:1];
[cell.aButton addTarget:self action:#selector(methodA:) forControlEvents:UIControlEventTouchUpInside];
[cell.aButton setTag:2];
[cell.bButton addTarget:self action:#selector(methodB:) forControlEvents:UIControlEventTouchUpInside];
}
Implement the button event handlers as-
- (void) methodA:(id) sender
{
UIView *view = [sender superView];
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateSelected];
UIButton *otherButton = [view viewWithTag:2];
[otherButton setImage:[UIImage imageNamed:#"default.png"] forState:UIControlStateNormal];
}
- (void) methodB:(id) sender
{
UIView * view = [sender superView];
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage:[UIImage imageNamed:#"b.png"] forState:UIControlStateSelected];
UIButton *otherButton = [view viewWithTag:1];
[otherButton setImage:[UIImage imageNamed:#"default.png"] forState:UIControlStateNormal];
}
Write a CustomCell class which has 2 buttons with target methods and delegate method to notify the table view when buttons clicked. Use this cell in the table view.
In target methods do the image changes(A/B) and fire the delegate method to notify the tableViewController.
You can keep indexpath as a property of cell and pass it with the delegate to tableView.(Set the indexpath for the cell in tableView's cellForRowAtIndexPath:)Then you can identify which cell's button was clicked and relevant data object can fetch from your data source.
CustomCell.m
- (IBAction)onButtonAClicked:(id)sender {
// change your images
[delegate cellButtonClicked:self.indexpath];
}
TableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
cell.delegate = self;
}
cell.indexpath = indexPath;
}
//delegate method
-(void)cellButtonClicked:(NSIndexPath *)indexPath{
//get the related data object for the cell using indexPath from your data source
}

CheckBox in UITableView

In UITableView, I want to put check uncheck buttons,when I click on first row[indexpath.row=0] remaining rows will uncheck and when click on indexpath.row>0 then row=0 unchceck. I have to do viceversa ,
intially I took an array with all objects uncheck and when check it that index will replace object check,
for(int i=0; i<[carwasharray count]; i++)
{
[arrayCheckUnchek addObject:#"Uncheck"];
}
/cell row at indexpath my code is
cell.textLabel.text = [carwasharray objectAtIndex:indexPath.row];
if (indexPath.row==0) {
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:[UIImage imageNamed:#"uncheck_icon.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(buttonClic:) forControlEvents:UIControlEventTouchUpInside];
button1.tag=1;
[button1 setFrame:CGRectMake(150.0, 7.0, 30.0, 30.0)];
[cell.contentView addSubview:button1];
}
else{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(150.0, 7.0, 30.0, 30.0)];
button.tag=2;
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:#"Uncheck"])
[button setImage:[UIImage imageNamed:#"uncheck_icon.png"] forState:UIControlStateNormal];
else
[button setImage:[UIImage imageNamed:#"check_icon"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
}
return cell;
}
-(void)buttonClic:(id)sender
{
//Getting the indexPath of cell of clicked button
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:carwashtable];
NSIndexPath *indexPath = [carwashtable indexPathForRowAtPoint:touchPoint];
UIButton *button = (UIButton *)sender;
if (button1.tag==1)
{
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:#"Uncheck"])
{
//[button1 setImage:[UIImage imageNamed:#"check_icon.png"] forState:UIControlStateNormal];
//[button setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
// [arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:#"Check"];
button.tag=3;
[self buttonClicked:button];
}
else
{
//[button1 setImage:[UIImage imageNamed:#"check_icon.png"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:#"Check"];
button.tag=3;
[self buttonClicked:button];
}
}
else if(button1.tag==0){
[button1 setImage:[UIImage imageNamed:#"uncheck_icon.png"] forState:UIControlStateNormal];
}
}
-(void)buttonClicked:(id)sender
{
//Getting the indexPath of cell of clicked button
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:carwashtable];
NSIndexPath *indexPath = [carwashtable indexPathForRowAtPoint:touchPoint];
// No need to use tag sender will keep the reference of clicked button
UIButton *button = (UIButton *)sender;
if (button.tag==2) {
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:#"Uncheck"])
{
[button setImage:[UIImage imageNamed:#"check_icon.png"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:#"uncheck_icon.png"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:#"Check"];
button1.tag=1;
}
else
{
[button setImage:[UIImage imageNamed:#"uncheck_icon.png"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:#"Uncheck"];
}
}
else
{
if ([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:#"Check"]) {
[button setImage:[UIImage imageNamed:#"uncheck_icon.png"] forState:UIControlStateNormal];
}
[button1 setImage:[UIImage imageNamed:#"check_icon.png"] forState:UIControlStateNormal];
}
now its working only 1st time when I click row=1, the row=0 wil uncheck, upto this ok, but when I recheck row=0 it will also check..
thank in advance
UITableView merely displays the UITableViewCell(s) you want it to.
I would recommend creating a Prototype cell that will hold your text and "CheckBox"
Here is a page that demonstrates a few different approaches to creating custom UITableViewCell objects.
http://mobile.tutsplus.com/tutorials/iphone/customizing-uitableview-cell/
Understanding how a task can be accomplished is as important as accomplishing the task itself.
Hi thanks every one who given me a reply.... I got the answer it working fine now
same code but slightly changes...
//cell for row at indexpath....
if (indexPath.row==0) {
[button1 addTarget:self action:#selector(completebuttonClic:) forControlEvents:UIControlEventTouchUpInside];
button1.tag=1;
[button1 setFrame:CGRectMake(240.0, 7.0,25.0, 25.0)];
[cell.contentView addSubview:button1];
}
else{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(240.0, 7.0, 25.0, 25.0)];
button.tag=2;
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:#"Uncheck"])
[button setImage:[UIImage imageNamed:#"uncheck_ico.png"] forState:UIControlStateNormal];
else
[button setImage:[UIImage imageNamed:#"check_icon.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
}
return cell;
}
//calling button targets
-(void)completebuttonClic:(id)sender
{
//completebutton clicked
//Getting the indexPath of cell of clicked button
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:carwashtable];
NSIndexPath *indexPath = [carwashtable indexPathForRowAtPoint:touchPoint];
UIButton *button = (UIButton *)sender;
if (button1.tag==1)
{
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:#"Uncheck"])
{
button.tag=3;
button1.tag=0;
[self buttonClicked:button];
}
else
{
button.tag=3;
[self buttonClicked:button];
}
}
else if(button1.tag==0){
[button1 setImage:[UIImage imageNamed:#"uncheck_ico.png"] forState:UIControlStateNormal];
[selectrows removeObject:[carwashtypeid objectAtIndex:indexPath.row]];
}
NSLog(#"%#",selectrows);
}
-(void)buttonClicked:(id)sender
{
//Getting the indexPath of cell of clicked button
CGPoint touchPoint = [sender convertPoint:CGPointZero tablviewname];
NSIndexPath *indexPath = [tableviewname indexPathForRowAtPoint:touchPoint];
// No need to use tag sender will keep the reference of clicked button
UIButton *button = (UIButton *)sender;
if (button.tag==2) {
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:#"Uncheck"])
{ NSLog(#"%ld",(long)indexPath.row);
[button setImage:[UIImage imageNamed:#"check_icon.png"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:#"uncheck_ico.png"] forState:UIControlStateNormal];
if (button1.imageView.image==[UIImage imageNamed:#"uncheck_ico.png"]) {
//adding selected rows in a selectrows array
[selectrows removeObject:[carwashtypeid objectAtIndex:0]];
}
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:#"Check"];
[selectrows addObject:[carwashtypeid objectAtIndex:indexPath.row]];
button1.tag=1;
}
else
{
[button setImage:[UIImage imageNamed:#"uncheck_ico.png"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:#"Uncheck"];
[selectrows removeObject:[carwashtypeid objectAtIndex:indexPath.row]];
}
}
else
{
for (int i=1; i<[carwasharray count]; i++) {
if ([[arrayCheckUnchek objectAtIndex:i] isEqualToString:#"Check"]) {
[arrayCheckUnchek replaceObjectAtIndex:i withObject:#"Uncheck"];
[carwashtable reloadData];
[selectrows removeObject:[carwashtypeid objectAtIndex:i]];
}}
[button1 setImage:[UIImage imageNamed:#"check_icon.png"] forState:UIControlStateNormal];
[selectrows addObject:[carwashtypeid objectAtIndex:indexPath.row]];
}
NSLog(#"%#",selectrows);
Having an array with string value is not very efficient way to know that current cell checked or unchecked status i would recommend you to create and custom cell and use a custom button class for check button like https://github.com/ardalahmet/SSCheckBoxView so you can ask from cell what is the status
This is how I would do it.
I would keep all selected cell's indices in an NSMutableArray, say selectedCellsArray.
In the didSelectRowAtIndexPath method I would first check if the selected cell is at index 0 using indexPath.row. If it is cell 0, I would do this:
[selectedCellsArray removeAllObjects];
[selectedCellsArray addObject:[NSNumber numberWithInt:0]];
If the selected cell's index is not 0, I would check if the selectedCellsArray contains 0, and remove it using:
if([selectedCellsArray containObject:[NSNumber numberWithInt:0]])
[selectedCellsArray removeObject:[NSNumber numberWithInt:0]];
And add the selected cell's index with:
[selectedCellsArray addObject:[NSNumber numberWithInt:indexPath.row]];
So the final code becomes:
if (indexPath.row == 0){
[selectedCellsArray removeAllObjects];
[selectedCellsArray addObject:[NSNumber numberWithInt:0]];
}else{
if([selectedCellsArray containObject:[NSNumber numberWithInt:0]])
[selectedCellsArray removeObject:[NSNumber numberWithInt:0]];
[selectedCellsArray addObject:[NSNumber numberWithInt:indexPath.row]];
}
[yourTableView reloadData];
In cellForRowAtIndexPath: I would check if the selectedCellsArray contains the current cell. If it does, change it's accessory type with :
if ([selectedCellsArray containObject:[NSNumber numberWithInt:indexPath.row]])
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
else
[cell setAccessoryType:UITableViewCellAccessoryNone];
The advantage here is that you can use the selectedCellsArray to process the information further.

How to change image for UIButton after user click on Done button is another UIView in iOS

When the user clicks on a UIButton on tableview ( tableview has mutiple button in each row), another View is shown.
I want to change image for this button after the user has clicked on the Done button of the other UIView. How can I do that? I'm a newbie. Could you please provide some code for me? Thanks in advance.
UPDATE CODE:
Code for tableview :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"%d,%d",indexPath.section,indexPath.row];
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[market setTag:3000];
[market setFrame:CGRectMake(200, 6, 30, 30)];
[cell.contentView addSubview:market];
}
for (UIButton *button in [cell subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
button.tag = indexPath.row;
[button setImage:[UIImage imageNamed:#"Marketplace.png"] forState:UIControlStateNormal];
}
}
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
_tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
return cell;
}
Code for Open button ( button that user click on to show another View)
- (void)marketPressedAction:(id)sender
{
UIButton *button = (UIButton *)sender;
buttontag = button.tag;
NSLog(#"Market button click at row %d",buttontag);
}
And code for Done button:
-(void)submitMarket
{
for (UIButton *button in [_tableView subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
if (button.tag == buttontag) {
[button setBackgroundImage:[UIImage imageNamed:#"MarketplaceSelect.png"] forState:UIControlStateNormal];
}
}
}
}
Try this :
[yourButton setBackgroundImage:someImge forState:UIControlStateNormal];
Code on button click :
savedTag = button.tag;
Code on Done button click :
for (UIButton *button in [table subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
if (button.tag == savedtag) {
[button setBackgroundImage:someImge forState:UIControlStateNormal];
}
}
}
In cellForRowAtIndexPath in place of this : [market setTag:3000]; write [market setTag:indexPath.row];
Try this :
Replace :
marketButton = (UIButton *)[cell.contentView viewWithTag:3000];
[marketButton setTag:indexPath.row];
With this :
for (UIButton *button in [cell subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
button.tag == indexPath.row;
}
}
One more thing : make first line of cellForRowAtIndexPath as :
NSString *CellIdentifier = #"tableCell";
Change your cellForRowAtIndexPath as :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[market setImage:[UIImage imageNamed:#"Marketplace.png"] forState:UIControlStateNormal];
[market setTag:indexPath.row];
[market setFrame:CGRectMake(200, 6, 30, 30)];
[cell.contentView addSubview:market];
}
else {
for (UIButton *button in [cell subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
button.tag = indexPath.row;
}
}
}
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
_tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
return cell;
}
you can use a delegate or a NSNotificationCenter,
in your view where is Done button, you need create a property
#property (weak, nonatomic) <protocol_name> delegate;
and in your action method for DoneButton, you need to send a message to that delegate
[self.delegate method_name];
and the other view will be set as delegate for this one
`[viewWithDoneButton setDelegate:self];
and you need to implement the delegate method `
You can set another image for selected state of your button and after click on Done button just set selected state your button.
[yourButton setBackgroundImage:someImge forState:UIControlStateSelected];
after Done button:
[yourButton setSelected:YES];
Or if you use before UIControlStateSelected you can also use any other state. Or even users state - UIControlStateApplication

Resources