Final UIButton in a UITableViewCell is unmutable - ios

I have a UITableView (and associated Cells) with a variable number of items. Inside each cell are values associated with the object being displayed. There is also a button allowing a user to state whether the task being displayed (from the object properties) is completed or not. It is supposed to change color and become active. This works, except for the final item on the list. Here is the code that is not working:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"TasksItem"];
[BAPAppDelegate setBorder:[cell viewWithTag:300] withBorder:[BAPAppDelegate darkGreyColor]];
[BAPAppDelegate setBorder:[cell viewWithTag:301] withBorder:[UIColor blackColor]];
[BAPAppDelegate setBorder:[cell viewWithTag:1000] withBorder:[UIColor blackColor]];
ServiceRequest *sr = (mListTasks)[indexPath.row];
[(UILabel *)[cell viewWithTag:110] setText:sr.vSRType];
UILabel *smry = (UILabel *)[cell viewWithTag:301];
smry.text = [NSString stringWithFormat:#"%#", [sr vMTSmry]]; // check if completed
int cnt;
int complete;
UIButton* btn;
cnt = [sr getBeforeRequired];
complete = [sr getBeforeCompleted];
markCompleteButton = YES;
btn = (UIButton *)[cell viewWithTag:120];
if (cnt > 0) {
[btn setHidden:NO];
[btn setTitle:[NSString stringWithFormat:#"%i", cnt] forState:UIControlStateNormal];
} else {
[btn setTitle:#"0" forState:UIControlStateNormal];
[btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
}
if (complete < cnt) {
[btn setTitleColor:[BAPAppDelegate redColor] forState:UIControlStateNormal];
[self markComplete:NO];
} else {
[btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
}
cnt = [sr getDuringRequired];
complete = [sr getDuringCompleted];
btn = (UIButton *)[cell viewWithTag:121];
if (cnt > 0) {
[btn setHidden:NO];
[btn setTitle:[NSString stringWithFormat:#"%i", cnt] forState:UIControlStateNormal];
} else {
[btn setTitle:#"0" forState:UIControlStateNormal];
[btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
}
if (complete < cnt) {
[btn setTitleColor:[BAPAppDelegate redColor] forState:UIControlStateNormal];
[self markComplete:NO];
} else {
[btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
}
cnt = [sr getAfterRequired];
complete = [sr getAfterCompleted];
btn = (UIButton *)[cell viewWithTag:122];
if (cnt > 0) {
[btn setHidden:NO];
[btn setTitle:[NSString stringWithFormat:#"%i", cnt] forState:UIControlStateNormal];
} else {
[btn setTitle:#"0" forState:UIControlStateNormal];
[btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
}
if (complete < cnt) {
[btn setTitleColor:[BAPAppDelegate redColor] forState:UIControlStateNormal];
[self markComplete:NO];
} else {
[btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
}
cnt = [sr getCheckRequired];
complete = [sr getCheckCompleted];
btn = (UIButton *)[cell viewWithTag:123];
if (complete < cnt) {
[btn setTitleColor:[BAPAppDelegate redColor] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:#"check_red.png"] forState:UIControlStateNormal];
[self markComplete:NO];
} else {
[btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:#"check.png"] forState:UIControlStateNormal];
}
btn = (UIButton *)[cell viewWithTag:1000];
[btn setTag:indexPath.row + 1001];
if (markCompleteButton) {
[btn setBackgroundColor:[BAPAppDelegate greenColor]];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn setEnabled:YES];
[btn setTitle:#"YEP" forState:UIControlStateNormal];
NSLog(#"YEP %ld", (long)indexPath.row);
} else {
[btn setEnabled:NO];
NSLog(#"NOPE %ld", (long)indexPath.row);
}
[cell prepareForReuse];
return cell;
}
As expected, the log displays "YEP 8", but nothing about the button itself changes. Other buttons earlier in the list display and perform their action as expected.
Any insight would be greatly appreciated!

I ended up subclassing UITableViewCell and connecting the UITableViewCell from my storyboard to my subclass, and then connecting the button's outlet to the subclass. Additionally, I set the button's action to go to the subclass.
TasksCell.h
#protocol TasksCellDelegate <NSObject>
- (void)buttonPressed:(UIButton *)sender;
#end
#interface TasksCell : UITableViewCell
#property (weak, nonatomic) id<TasksCellDelegate> delegate;
#property (weak, nonatomic) IBOutlet UIButton *button;
- (IBAction)buttonPressed:(UIButton *)sender;
#end
TasksCell.m:
#import "TasksCell.h"
#implementation TasksCell
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (IBAction)buttonPressed:(UIButton *)sender {
[self.delegate buttonPressed:sender];
}
#end
TasksViewController.h
#import "TasksCell.h"
TasksViewController.m:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TasksCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:#"TasksItem"
forIndexPath:indexPath];
[BAPAppDelegate setBorder:[cell viewWithTag:300] withBorder:[BAPAppDelegate darkGreyColor]];
[BAPAppDelegate setBorder:[cell viewWithTag:301] withBorder:[UIColor blackColor]];
[BAPAppDelegate setBorder:[cell viewWithTag:1000] withBorder:[UIColor blackColor]];
ServiceRequest *sr = (mListTasks)[indexPath.row];
[(UILabel *)[cell viewWithTag:110] setText:sr.vSRType];
UILabel *smry = (UILabel *)[cell viewWithTag:301];
smry.text = [NSString stringWithFormat:#"%#", [sr vMTSmry]]; // check if completed
int cnt;
int complete;
UIButton* btn;
cnt = [sr getBeforeRequired];
complete = [sr getBeforeCompleted];
markCompleteButton = YES;
btn = (UIButton *)[cell viewWithTag:120];
if (cnt > 0) {
[btn setHidden:NO];
[btn setTitle:[NSString stringWithFormat:#"%i", cnt] forState:UIControlStateNormal];
} else {
[btn setTitle:#"0" forState:UIControlStateNormal];
[btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
}
if (complete < cnt) {
[btn setTitleColor:[BAPAppDelegate redColor] forState:UIControlStateNormal];
[self markComplete:NO];
} else {
[btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
}
cnt = [sr getDuringRequired];
complete = [sr getDuringCompleted];
btn = (UIButton *)[cell viewWithTag:121];
if (cnt > 0) {
[btn setHidden:NO];
[btn setTitle:[NSString stringWithFormat:#"%i", cnt] forState:UIControlStateNormal];
} else {
[btn setTitle:#"0" forState:UIControlStateNormal];
[btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
}
if (complete < cnt) {
[btn setTitleColor:[BAPAppDelegate redColor] forState:UIControlStateNormal];
[self markComplete:NO];
} else {
[btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
}
cnt = [sr getAfterRequired];
complete = [sr getAfterCompleted];
btn = (UIButton *)[cell viewWithTag:122];
if (cnt > 0) {
[btn setHidden:NO];
[btn setTitle:[NSString stringWithFormat:#"%i", cnt] forState:UIControlStateNormal];
} else {
[btn setTitle:#"0" forState:UIControlStateNormal];
[btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
}
if (complete < cnt) {
[btn setTitleColor:[BAPAppDelegate redColor] forState:UIControlStateNormal];
[self markComplete:NO];
} else {
[btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
}
cnt = [sr getCheckRequired];
complete = [sr getCheckCompleted];
btn = (UIButton *)[cell viewWithTag:123];
if (complete < cnt) {
[btn setTitleColor:[BAPAppDelegate redColor] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:#"aprsi_check_red.png"] forState:UIControlStateNormal];
[self markComplete:NO];
} else {
[btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:#"aprsi_check.png"] forState:UIControlStateNormal];
}
[cell.button setTag:indexPath.row + 1001];
if (markCompleteButton) {
cell.button.backgroundColor = [BAPAppDelegate greenColor];
[cell.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[cell.button setEnabled:YES];
NSLog(#"YEP %ld", (long)indexPath.row);
} else {
[cell.button setTitleColor:[BAPAppDelegate darkGreyColor] forState:UIControlStateDisabled];
[cell.button setBackgroundColor:[BAPAppDelegate lightGreyColor]];
[cell.button setEnabled:NO];
NSLog(#"NOPE %ld", (long)indexPath.row);
}
[cell prepareForReuse];
return cell;
}
Which allowed the button to work as expected, I kept the button delegate in the TasksViewController, but just as easily could move it to the subclass. Hope this helps someone out!

Related

how to save multiple selected buttons in custom cell?

in my custom cell lot of question and answers are there, in each section i want to select any one button,that button how can store it? in CellForRowAtIndexPath i created targets for each answer,
here is my code!
-(void)buttonsClicked:(id)sender
{
UIButton *btn=(UIButton*)sender;
ContestQATableViewCell * cell=(ContestQATableViewCell *)[btn.superview superview];
if (cell.answer1.tag==btn.tag)
{
NSLog(#"%ld",(long)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)
{
NSLog(#"%ld",(long)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)
{
NSLog(#"%ld",(long)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)
{
NSLog(#"%ld",(long)btn.tag);
[cell.answer4 setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
}else{
[cell.answer4 setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
}
}
You need add one key your datasource array and modify that key value depend upon button state on click action. Check below code for your reference
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyCustomTableViewCell *cell = (MyCustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"MyCustomTableViewCell"];
[cell.btnSelect addTarget:self action:#selector(btnSelectClick:) forControlEvents:UIControlEventTouchUpInside];
cell.btnSelect.selected = [[[self.arraySource objectAtIndex:indexPath.row] valueForKey:#"isSleected"] boolValue];
cell.btnSelect.tag = indexPath.row;
return cell;
}
- (void)btnSelectClick:(UIButton *)sender {
sender.selected = !sender.selected;
NSMutableDictionary *dicTemp = [NSMutableDictionary dictionaryWithDictionary:[self.arraySource objectAtIndex:sender.tag]];
[dicTemp setValue:[NSNumber numberWithBool:sender.selected] forKey:#"isSleected"];
[self.arraySource replaceObjectAtIndex:sender.tag withObject:dicTemp];
}
Here is arraySource in view did load
self.arraySource = [NSMutableArray array];
[self.arraySource addObject:#{#"name":#"name-1"}];
[self.arraySource addObject:#{#"name":#"name-2"}];
[self.arraySource addObject:#{#"name":#"name-3"}];

How to create two radio buttons on a uitableview cell

I have created two radio buttons on one tableview cell.That are options for a question,But when I select them they both are get selected that I don't want,I want to select only one of them but I am not able to do that......Please help me Here is my code for customCell
- (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 setTag:0];
[leftBtnclick setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[leftBtnclick setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateSelected];
[leftBtnclick setFrame:CGRectMake(50, 120, 30, 30)];
[leftBtnclick addTarget:self action:#selector(leftTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:leftBtnclick];
rightBtnclick = [UIButton buttonWithType:UIButtonTypeCustom];
[leftBtnclick setTag:1];
[rightBtnclick setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[rightBtnclick setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateSelected];
[rightBtnclick setFrame:CGRectMake(180, 120, 30, 30)];
[rightBtnclick addTarget:self action:#selector(rightTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:rightBtnclick];
cell.numberLbl.text = [numberArray objectAtIndex:indexPath.row];
return cell;
}
-(void)leftTickBtnClicked:(id)sender
{
if ([leftBtnclick isSelected]) {
[sender setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
else
{
[sender setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
}
}
-(void)rightTickBtnClicked:(id)sender
{
if ([sender isSelected])
{
[sender setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
else
{
[sender setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
}
}
Firstly , set leftBtnclick.tag=999 and rightBtnclick.tag=1000.
And then add this code:-
-(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;
}
}
}
}
Add four buttons on custom table cell IBOutlet it,and write this code in cellforrowatindexpath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *TableIdentifier = #"CELL";
questionAireCell *cell = [questionAireTable dequeueReusableCellWithIdentifier:TableIdentifier];
if (cell == nil)
{
NSArray *obj = [[NSBundle mainBundle] loadNibNamed:#"questionAireCell" owner:self options:nil];
cell = [obj objectAtIndex:0];
}
cell.questionText.text=[cell.questionText.text stringByAppendingFormat:#"%ld",indexPath.row+1];
cell.commentText.userInteractionEnabled=YES;
cell.commentText.editable=YES;
cell.commentText.tag = indexPath.row;
cell.commentText.delegate = self;
NSLog(#"Question Count=%ld",questionDescriptionArr.count);
cell.questionText.text = [questionDescriptionArr objectAtIndex:indexPath.row];
NSString *number = [[[NSString alloc]initWithString:[srNumberArr objectAtIndex:indexPath.row]]stringByAppendingString:#"."];
cell.questionNoLbl.text = number;
[cell.rightOptBtn addTarget:self action:#selector(rightOptBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.rightTickBtn addTarget:self action:#selector(rightOptBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.leftOptBtn addTarget:self action:#selector(leftOptBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.leftTickBtn addTarget:self action:#selector(leftOptBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
// [cell.rightOptBtn addTarget:self action:#selector(rightOptBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
// [cell.rightTickBtn addTarget:self action:#selector(rightOptBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
// [cell.leftOptBtn addTarget:self action:#selector(leftOptBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
// [cell.leftTickBtn addTarget:self action:#selector(leftOptBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
if ([[answerArray objectAtIndex:indexPath.row] isEqualToString:#"YES"])
{
[cell.leftTickBtn setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
[cell.rightTickBtn setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
else if ([[answerArray objectAtIndex:indexPath.row] isEqualToString:#"NO"])
{
[cell.leftTickBtn setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[cell.rightTickBtn setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
}
else
{
[cell.leftTickBtn setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[cell.rightTickBtn setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
return cell;
}
-(void)leftOptBtnClicked:(UIButton *)sender
{
NSLog(#"Left btn clicked");
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:questionAireTable];
NSIndexPath *indexPath1 = [questionAireTable indexPathForRowAtPoint:buttonPosition];
[answerArray replaceObjectAtIndex:indexPath1.row withObject:#"YES"];
[questionAireTable reloadData];
NSLog(#"Answer Array: %#",answerArray);
}
-(void)rightOptBtnClicked:(UIButton *)sender
{
NSLog(#"Left btn clicked");
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:questionAireTable];
NSIndexPath *indexPath1 = [questionAireTable indexPathForRowAtPoint:buttonPosition];
[answerArray replaceObjectAtIndex:indexPath1.row withObject:#"NO"];
[questionAireTable reloadData];
NSLog(#"Answer Array: %#",answerArray);
}

UITableview button image disappear while scroll

I am having an UITableview in that in that having three buttons, and the buttons having conditions them working well. My problem is when I select any button image change to the selected image. but when I scroll the table view selected image are changed to normal image. I want to save the selected in the table view. I have seen many stack overflow, but its not helping me. any one help me please.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"S%1dR%1d",indexPath.section,indexPath.row];
CustomizedCellView *cell = (CustomizedCellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [table dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomizedCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(80, 27, 36, 36);
[button1 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"l"ofType:#"png"]] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:#"lblue.png"]
forState:UIControlStateSelected];
[button1 addTarget:self action:#selector(radiobtn4:) forControlEvents:UIControlEventTouchUpInside];
button1.tag=1;
[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 setImage:[UIImage imageNamed:#"eblue.png"]
forState:UIControlStateSelected];
[button2 addTarget:self action:#selector(radiobtn4:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button2];
button2.tag=3;
button3 = [UIButton buttonWithType:UIButtonTypeCustom];
button3.frame = CGRectMake(240, 27, 36, 36);
[button3 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"v"ofType:#"png"]] forState:UIControlStateNormal];
[button3 setImage:[UIImage imageNamed:#"vblue.png"]
forState:UIControlStateSelected];
[button3 addTarget:self action:#selector(radiobtn4:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button3];
button3.tag=2;
- (void)radiobtn4:(UIButton *)sender
{
UITableViewCell *cell=(UITableViewCell *)sender.superview;
if(sender.selected == NO)
{
if(sender.tag==1){
UIButton *otherButton=(UIButton *)[cell viewWithTag:1];
otherButton.selected=YES;
UIButton *other=(UIButton *)[cell viewWithTag:2];
other.selected=NO;
}else if(sender.tag==2){
UIButton *otherButton=(UIButton *)[cell viewWithTag:2];
otherButton.selected=YES;
UIButton *other=(UIButton *)[cell viewWithTag:1];
other.selected=NO;
} else if(sender.tag == 3)
{
UIButton *otherButton=(UIButton *)[cell viewWithTag:3];
otherButton.selected=YES;
}
} else if(sender.selected == YES)
{
if(sender.tag==1){
UIButton *otherButton=(UIButton *)[cell viewWithTag:1];
[otherButton setImage:[UIImage imageNamed:#"l.png"]
forState:UIControlStateNormal];
otherButton.selected=NO;
}else if(sender.tag==2){
UIButton *otherButton=(UIButton *)[cell viewWithTag:2];
[otherButton setImage:[UIImage imageNamed:#"v.png"]
forState:UIControlStateNormal];
otherButton.selected=NO;
} else if(sender.tag == 3)
{
UIButton *otherButton=(UIButton *)[cell viewWithTag:3];
[otherButton setImage:[UIImage imageNamed:#"e.png"]
forState:UIControlStateNormal];
otherButton.selected=NO;
}
}
}
above is my coding, i want to use any array to store the selected image,how can i do that process, please help me in coding.

how to check the condition for UIButtons in UITableview

I am having three button image in table view, I want to check the conditions between them. when I click the button 1 means background image change to blue colour. at the same time I click the button 1 it will move to normal state white colour. Same for another two buttons.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath![button3 condition][3]
{
static NSString *cellIdentifier = #"HistoryCell";
CustomizedCellView *cell = (CustomizedCellView *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[CustomizedCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
UIButton *button1;
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 = 1;
[button1 addTarget:self action:#selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
[button1 setSelected:true];
[cell.contentView addSubview:button1];
UIButton *button2;
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 = 1;
[button2 addTarget:self action:#selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
[button2 setSelected:true];
[cell.contentView addSubview:button2];
UIButton *button3;
button3 = [UIButton buttonWithType:UIButtonTypeCustom];
button3.frame = CGRectMake(240, 27, 36, 36);
[button3 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"v"ofType:#"png"]] forState:UIControlStateNormal];
button3.tag = 1;
[button3 addTarget:self action:#selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
[button3 setSelected:true];
[cell.contentView addSubview:button3];
return cell;
}
My condition is: when button 1 is clicked means button 3 should not change. when button 3 is clicked means button 1 should not change.button 2 can select in both the condition.
- (void)radiobtn:(UIButton *)button
{
if(button.tag == 1)
{
[button setImage:[UIImage imageNamed:#"lblue.png"] forState:UIControlStateSelected];
}
if(button.tag == 2)
{
[button setImage:[UIImage imageNamed:#"eblue.png"] forState:UIControlStateSelected];
}
if(button.tag == 3)
{
[button setImage:[UIImage imageNamed:#"vblue.png"] forState:UIControlStateSelected];
}
}
can any one help me in coding.
First of all you can set different tag for each button's like
button.tag == 1,
button.tag == 2,
button.tag == 3
And then after you can write your radiobtn Action like this way..
-(IBAction) radiobtn:(id)sender
{
UIButton *yourBtn = (UIButton *)[self.view viewWithTag:[sender tag]];
if(yourBtn.tag == 1) {
[yourBtn setImage:[UIImage imageNamed:#"lblue.png"] forState:UIControlStateSelected];
}
else if(yourBtn.tag == 2){
[yourBtn setImage:[UIImage imageNamed:#"eblue.png"] forState:UIControlStateSelected];
}
else{
[yourBtn setImage:[UIImage imageNamed:#"vblue.png"] forState:UIControlStateSelected];
}
}
button1.tag=1; button2.tag=2; button3.tag=3;
-(void)radiobtn:(UIButton *)button
{
if(button.tag==1)
{
if(![button3 isSelected])
{
if([button1 isSelected])
button1.backgroundColor = [UIColor blueColor];
else
button1.backgroundColor = [UIColor whiteColor];
}
else
{
//No change
}
}
else if(button.tag==2)
{
if([button2 isSelected])
button2.backgroundColor = [UIColor blueColor];
else
button2.backgroundColor = [UIColor whiteColor];
}
else if(button.tag==3)
{
if(![button1 isSelected])
{
if([button3 isSelected])
button3.backgroundColor = [UIColor blueColor];
else
button3.backgroundColor = [UIColor whiteColor];
}
else
{
//No change
}
}
}
Can you have a try on this..
Please check!! i have done same thing on my code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"UITableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
UIView * selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
[selectedBackgroundView setBackgroundColor:[UIColor redColor]]; // set color here
[cell setSelectedBackgroundView:selectedBackgroundView];
self.tableview.separatorColor = [UIColor whiteColor];
if (_userResult.relationStatus == [arrAnswrs objectAtIndex:indexPath.row]){
cell.accessoryType=UITableViewCellAccessoryCheckmark;
}else if (_userResult.childrenStatus == [arrAnswrs objectAtIndex:indexPath.row]){
cell.accessoryType=UITableViewCellAccessoryCheckmark;
}
else{
cell.accessoryType=UITableViewCellAccessoryNone;
}
[[UITableViewCell appearance] setTintColor:[UIColor redColor]];
cell.textLabel.text = [arrAnswrs objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont fontWithName:#"Roboto" size:16];
cell.textLabel.textColor = UIColorFromRGB(0xe212121);
cell.backgroundColor = UIColorFromRGB(0xeaaea7);
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"Select indexPath.section %d --- index %d", (int)indexPath.section, (int)indexPath.row);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *status = [arrAnswrs objectAtIndex:indexPath.row];
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor redColor];
[cell setSelectedBackgroundView:bgColorView];
int iSeletedButton = (int)selectButton.tag ;
NSLog(#"value %# -- iSeletedButton %d", [arrAnswrs objectAtIndex:indexPath.row], iSeletedButton);
switch (iSeletedButton) {
case 1:
_userResult.relationStatus = status;
_labelFrstStatus.text = status;
_labelFrstStatus.textColor = [UIColor blackColor];
break;
case 2:
_userResult.childrenStatus = status;
_labelSecndStatus.text = status;
_labelSecndStatus.textColor = [UIColor blackColor];
break;
default:
break;
}
if (_userResult.relationStatus || _userResult.childrenStatus) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
else cell.accessoryType = UITableViewCellAccessoryCheckmark;
[tableView reloadData];
}
U can set tags to button
button1.tag=1;
button2.tag=2;
button3.tag=3; //instead of tag 1 for all buttons
Take reference for buttons as button1,button2,button3 globally..
NSLog(#"%#",[[sender superview] class]); //UITableViewCellContentView
NSLog(#"%#",[[[sender superview] superview] class]); //UITableViewCellScrollView
NSLog(#"%#",[[[[sender superview]superview]superview] class]); //UITableViewCell
- (void)radiobtn:(UIButton *)button
{
if(button.tag==1)
{
//handle
CustomizedCellView * cell = (CustomizedCellView*)[[[button superview]superview]superview];
if ([button.imageView.image isEqual:[UIImage imageNamed:#"lblue.png"]]) {
[button setImage:[UIImage imageNamed:#"lwhite.png"] forState:UIControlStateSelected];
}
for (UIButton *btn in cell.contentView.subviews) {
if (btn.tag==3) {
[btn setImage:[UIImage imageNamed:#"vwhite.png"] forState:UIControlStateSelected];
}
}
}
else if(button.tag==2)
{
//handle
if ([button.imageView.image isEqual:[UIImage imageNamed:#"eblue.png"]]) {
[button setImage:[UIImage imageNamed:#"ewhite.png"] forState:UIControlStateSelected];
}
}
else if(button.tag==3)
{
//handle
CustomizedCellView * cell = (CustomizedCellView*)[[[sender superview]superview]superview];
if ([button.imageView.image isEqual:[UIImage imageNamed:#"vblue.png"]]) {
[button setImage:[UIImage imageNamed:#"vwhite.png"] forState:UIControlStateSelected];
}
for (UIButton *btn in cell.contentView.subviews) {
if (btn.tag==2) {
[btn setImage:[UIImage imageNamed:#"lwhite.png"] forState:UIControlStateSelected];
}
}
}
}
Hope it helps you...!

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.

Resources