How to display checked images while scrolling the UITableView? - ios

UITable view is working fine. when scrolling checked image is changed to uncheck. And also i need to take the particular checked image data in SAVE button Action.Can any body help me to solve this problem in prj.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle= UITableViewCellSelectionStyleNone;
}
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(10,10, 20, 20)];
[btn setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal]; [cell addSubview:btn];
btn.tag=4;
forState:UIControlStateNormal];
[btn addTarget:self action:#selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];
UILabel *lbl_name =[[UILabel alloc]initWithFrame:CGRectMake(35, 10, 100, 20)];
lbl_name.text=[NSString stringWithFormat:#"%#",[[arr1 valueForKey:#"Name"]objectAtIndex:indexPath.row]];
lbl_name.tag=5;
lbl_name.textColor=[UIColor blackColor];
[lbl_name setTextAlignment:NSTextAlignmentCenter];
lbl_name.font=[UIFont systemFontOfSize:15];
[cell addSubview:lbl_name];
return cell;
}
-(void)checkBoxClicked:(id)sender
{
UIButton *tappedButton = (UIButton*)sender;
if([tappedButton.currentImage isEqual:[UIImage imageNamed:#"unchecked.png"]])
{
[sender setImage:[UIImage imageNamed: #"checked.png"] forState:UIControlStateNormal];
} else {
[sender setImage:[UIImage imageNamed:#"unchecked.png"]forState:UIControlStateNormal];
}
}

Take one array which is Use to store IndexValue of Selected button,
#property (nonatomic,retain) NSMutableArray *arySelected;
Initialize array in ViewDidLoad,
- (void)viewDidLoad {
[super viewDidLoad];
self.arySelected = [[NSMutableArray alloc] init];
}
Change Code of TableViewCell Delegate,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle= UITableViewCellSelectionStyleNone;
}
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(10,10, 20, 20)];
if (![arySelected containsObject:[NSString stringWithFormat:#"%ld",(long)indexPath.row]])
{
[btn setImage:[UIImage imageNamed: #"checked.png"] forState:UIControlStateSelected];
}
else
{
[btn setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
btn.tag=indexPath.row;
[btn addTarget:self action:#selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
UILabel *lbl_name =[[UILabel alloc]initWithFrame:CGRectMake(35, 10, 100, 20)];
lbl_name.text=[NSString stringWithFormat:#"%#",[[arr1 valueForKey:#"Name"]objectAtIndex:indexPath.row]];
lbl_name.tag=5;
lbl_name.textColor=[UIColor blackColor];
[lbl_name setTextAlignment:NSTextAlignmentCenter];
lbl_name.font=[UIFont systemFontOfSize:15];
[cell addSubview:lbl_name];
return cell;
}
// On your button Event,
-(void)checkBoxClicked:(id)sender
{
sender.selected = ! sender.selected;
if (sender.selected)
{
[arySelected addObject:[NSString stringWithFormat:#"%ld",(long)sender.tag]];
}
else
{
[arySelected removeObject:[NSString stringWithFormat:#"%ld",(long)sender.tag]];
}
[self.tableView reloadData];
}

you use the deferent state to set image like this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle= UITableViewCellSelectionStyleNone;
}
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(10,10, 20, 20)];
[btn setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed: #"checked.png"] forState:UIControlStateSelected];
btn.selected=NO;
btn.tag=4;
[btn addTarget:self action:#selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
UILabel *lbl_name =[[UILabel alloc]initWithFrame:CGRectMake(35, 10, 100, 20)];
lbl_name.text=[NSString stringWithFormat:#"%#",[[arr1 valueForKey:#"Name"]objectAtIndex:indexPath.row]];
lbl_name.tag=5;
lbl_name.textColor=[UIColor blackColor];
[lbl_name setTextAlignment:NSTextAlignmentCenter];
lbl_name.font=[UIFont systemFontOfSize:15];
[cell addSubview:lbl_name];
return cell;
}
-(void)checkBoxClicked:(id)sender
{
UIButton *tappedButton = (UIButton*)sender;
if (tappedButton.isSelected) {
tappedButton.selected=NO;
}
else
{
tappedButton.selected=YES;
}
}

Related

Button Getting Populating Some Other Cell in UITableViewCell using Objective C?

I have created UITableview Programmatically and Populated array values dynamically. Here My Problem is, I created a UIButton Programmatically for zeroth row, It created well but When I am Scrolling the tableview button is Populating some other cells also. I don't Know how to solve that issue in my tableview. I want a UIButton on Only Zeroth index of the cell. I have tried the below code..
- (IBAction)MoreBttn:(id)sender {
NSUserDefaults *userDefaults1 = [NSUserDefaults standardUserDefaults];
[userDefaults1 setObject:amenties forKey:#"FACILITIES"];
CGRect screenRect = [[UIScreen mainScreen] bounds];
amentiestableview=[[UITableView alloc]initWithFrame:screenRect style:UITableViewStylePlain];
amentiestableview.delegate=self;
amentiestableview.dataSource=self;
UIEdgeInsets insets = UIEdgeInsetsMake(20,10,20,10);
CGRect tableframe=amentiestableview.frame;
amentiestableview.frame=UIEdgeInsetsInsetRect(tableframe,insets);
[[UIApplication sharedApplication].keyWindow addSubview:amentiestableview];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [amenties count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"facilities";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if(indexPath.row == 0) {
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
float X_Co = self.tableView.frame.size.width - 60;
[b setFrame:CGRectMake(X_Co, 0.0, 60, 45)];
[b setTitle:#"button" forState:UIControlStateNormal];
[b addTarget:self
action:#selector(hidetableview:)
forControlEvents:UIControlEventTouchUpInside];
[b setImage:[UIImage imageNamed:#"clearpink.png"] forState:UIControlStateNormal];
[cell addSubview:b];
}
else{
}
cell.textLabel.text=[amenties objectAtIndex:indexPath.row];
return cell;
}
-(void)hidetableview:(UIButton*)sender
{
amentiestableview.hidden=YES;
}
The cell is being re-used, so the button appear on other cells. Try to update cellForRow method as follows:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
float X_Co = self.tableView.frame.size.width - 60;
[b setFrame:CGRectMake(X_Co, 0.0, 60, 45)];
[b setTitle:#"button" forState:UIControlStateNormal];
[b addTarget:self
action:#selector(hidetableview:)
forControlEvents:UIControlEventTouchUpInside];
[b setImage:[UIImage imageNamed:#"clearpink.png"] forState:UIControlStateNormal];
[cell addSubview:b];
}
if(indexPath.row == 0) {
b.hidden = false
}
else{
b.hidden = true
}
We are recycling the cells by using the method dequeueReusableCellWithIdentifier. And you should recycle cells because the allocation of views need a lot of resources
Please try the below code
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if(indexPath.row == 0) {
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
float X_Co = self.tableView.frame.size.width - 60;
[b setFrame:CGRectMake(X_Co, 0.0, 60, 45)];
[b setTitle:#"button" forState:UIControlStateNormal];
[b addTarget:self
action:#selector(hidetableview:)
forControlEvents:UIControlEventTouchUpInside];
[b setImage:[UIImage imageNamed:#"clearpink.png"] forState:UIControlStateNormal];
b.tag = 1010
[cell addSubview:b];
}
else{
cell.contentView.viewWithTag(1010).removeFromSuperview()
}

changing UIButton image background in Table view cell for particular row base on som condition

I have a custom table view in my app which has button in each row.
I want to change button background image when flag of that cell is i'1'(check if statement in code.).
it is successfully executing if statements.
Just the button's background image is not changing.
Here is my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"cell"];
cell= [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"]autorelease];
if(tableView == tablePatchFilter)
{
cell.textLabel.text=[filterPatchName objectAtIndex:indexPath.row];
}
else
{
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text=[displayItems objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UILabel *typeLabel = [[[UILabel alloc]init]autorelease];
typeLabel.backgroundColor = [UIColor clearColor];
typeLabel.text = [displayItemsType objectAtIndex:indexPath.row];
typeLabel.textAlignment = NSTextAlignmentLeft;
typeLabel.font=[UIFont fontWithName:MyFont size:17.f];
typeLabel.frame = CGRectMake(300,2.0f,250,50);
[cell addSubview:typeLabel];
UILabel *typeLabel1 = [[[UILabel alloc]init]autorelease];
typeLabel1.backgroundColor = [UIColor clearColor];
typeLabel1.text = [displayItemPatch objectAtIndex:indexPath.row];
typeLabel1.textAlignment = NSTextAlignmentLeft;
typeLabel1.font=[UIFont fontWithName:MyFont size:17.f];
typeLabel1.frame = CGRectMake(550,2.0f,250,50);
[cell addSubview:typeLabel1];
UILabel *typeLabel2 = [[[UILabel alloc]init]autorelease];
typeLabel2.backgroundColor = [UIColor clearColor];
typeLabel2.font=[UIFont fontWithName:MyFont size:17.f];
typeLabel2.text = [displayItemclass objectAtIndex:indexPath.row];
typeLabel2.textAlignment = NSTextAlignmentLeft;
typeLabel2.frame = CGRectMake(730,2.0f,250,50);
[cell addSubview:typeLabel2];
int selectedSegment = mainSegment.selectedSegmentIndex;
if(selectedSegment == 0)
{
for(int i = 0;i<[displayFlag count];i++)
{ UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.tag=indexPath.row;
[btn addTarget:self
action:#selector(takeSignature:)
forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
NSMutableString *pathhh= [NSMutableString stringWithFormat:#"%#",[displayFlag objectAtIndex:i]];
NSLog(#"XXXXXXXXXXXXXXXXXXXXXXXXXX%#",pathhh);
if([pathhh isEqualToString:#"1"])
{
NSLog(#"in IFFFFFFF%#",pathhh);
btn.frame = CGRectMake(850,1, 50, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"signature.png"]
forState:UIControlStateNormal];
}
else
{
NSLog(#"in ELSSSSSSSSSS %#",pathhh);
btn.frame = CGRectMake(850,1, 50, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"add.png"]
forState:UIControlStateNormal];
}
}
/*
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(850,1, 50, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"signature.png"]
forState:UIControlStateNormal];
[btn addTarget:self
action:#selector(takeSignature:)
forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
*/
}
}
cell.textLabel.font=[UIFont fontWithName:MyFont size:17.f];
return cell;
}
In your implementation every time you are creating new cell, after getting cell from "dequeueReusableCellWithIdentifier" you have to check for cell if it nil than only create new cell using "alloc-init".
The better implementation of the particular senireo is to take a custom UITableViewCell.
The following stategie you should follow.
In the custom cell create the outlet for label 1, 2 and 3, than set the test in cellForRowatIndexPath.
Create a property of NSMutableArray in the custom cell class.
After getting the cell from "dequeueReusableCellWithIdentifier" check if cell is nil than only create the buttons and add all the buttons in the NSMutableArray created in step 2.
take the buttons from the NSMutableArray and that apply the condition for setting the images.
Please see the below code:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(tableView == tablePatchFilter)
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
cell.textLabel.text=[filterPatchName objectAtIndex:indexPath.row];
return cell;
} else {
CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"cell"];
if (!cell) {
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
int selectedSegment = mainSegment.selectedSegmentIndex;
if(selectedSegment == 0) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.tag=indexPath.row;
[btn addTarget:self action:#selector(takeSignature:) forControlEvents:UIControlEventTouchUpInside];
[cell.buttonArray addObject:btn];
}
}
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text=[displayItems objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.label1.text = [displayItemsType objectAtIndex:indexPath.row];
cell.label2.text = [displayItemPatch objectAtIndex:indexPath.row];
cell.label3.text = [displayItemclass objectAtIndex:indexPath.row];
for (UIButton *btn in cell.buttonArray) {
NSMutableString *pathhh= [NSMutableString stringWithFormat:#"%#",[displayFlag objectAtIndex:i]];
if([pathhh isEqualToString:#"1"]) {
btn.frame = CGRectMake(850,1, 50, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"signature.png"] forState:UIControlStateNormal];
} else {
btn.frame = CGRectMake(850,1, 50, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"add.png"] forState:UIControlStateNormal];
}
}
return cell;
}
}

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

Radio button inside the UITableview not working

I wish to add radio button to UITableview. The function of radio button should be like when I select radio button in the table cell that radio button should be selected and other all radio buttons in the other cell should be unselected. It should be kind of either or, that is only one radio button can select.
create the one int value
int selectstr;
UIButton * button;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
UILabel * title = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 14.0, 215.0, 36.0)];
title.backgroundColor = [UIColor clearColor];
title.textAlignment = NSTextAlignmentLeft;
title.textColor=[UIColor blackColor];
title.tag=indexPath.row;
title.text = #"TEST";
// title.font = [UIFont fontWithName:#"Arial Hebrew" size:16.0f];
title.font = [UIFont fontWithName:#"Palatino-Roman" size:14.0];
[cell.contentView addSubview:title];
button = [[UIButton alloc] initWithFrame:CGRectMake(20.0f, 20.0f, 46.0f, 30.0f)];
button.tag=indexPath.row;
if (selectstr ==indexPath.row)
{
[button setImage:[UIImage imageNamed:#"radio_selected.png"] forState:UIControlStateNormal];
cell.backgroundColor=[UIColor colorWithRed:(243.0/255.0) green:(114.0/255.0) blue:(74.0/255.0) alpha:1.0f];
}
else
{
[button setImage:[UIImage imageNamed:#"radio_unselected.png"] forState:UIControlStateNormal];
cell.backgroundColor=[UIColor clearColor];
}
button.adjustsImageWhenHighlighted=NO;
[button addTarget:self action:#selector(toggleCheckedMode:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectstr=indexPath.row;
[tableView reloadData];
}
- (IBAction)toggleCheckedMode:(UIButton*)sender
{
[button setImage:[UIImage imageNamed:#"radio_selected.png"] forState:UIControlStateNormal];
// here customized for your self
selectstr=sender.tag;
[tableView reloadData];
}
Write this code into
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
NSIndexPath *selectedIndex;
UIButton *radioButton;
radioButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
radioButton.frame = CGRectMake(30,23,24,24);
// [radioButton setTitle:#"" forState:UIControlStateNormal];
radioButton.tag = 8888;
[cell.contentView addSubview:radioButton];
radioButton = (UIButton *)[cell.contentView viewWithTag:8888];
if (selectedIndex && selectedIndex.row == indexPath.row)
{
[radioButton setBackgroundImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
}
else
{
[radioButton setBackgroundImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedIndex =indexPath;
[self.userTabelView reloadData];
}

Can not change image uibutton in uitableview after reloadData iOS

I create a tableview has button, this button has been changed image ass different condition. The first load tableview, these buttons set image correct but after condition change, it set image incorrect. Example : If [Selling = 0] , set image "Market.png", else [Selling =2], set image "MarketSelect.png". When value of Selling array changing, i used [_tableview reloadData] but it still not change image. I debug, i see MarketButton is 0x0000. The first load, MarketButton works correct but second load, it is 0x0000. Please give me advice to solve this problem. I tried 2 days but it can not works. thanks much
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (nil == cell) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.button.tag = indexPath.row;
UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
[market setFrame:CGRectMake(200, 6, 30, 30)];
market.tag = 4000;
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[cell.contentView addSubview:market];
UILabel *pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 80, 30)];
pricelabel.backgroundColor = [UIColor clearColor];
pricelabel.font = [UIFont fontWithName:#"Helvetica" size:16];
pricelabel.font = [UIFont boldSystemFontOfSize:16];
pricelabel.textColor = [UIColor darkGrayColor];
pricelabel.tag = 3000;
//pricelabel.hidden = YES;
pricelabel.textAlignment = NSTextAlignmentRight;
[cell.contentView addSubview: pricelabel];
[pricelabel release];
}
UIButton *marketButton = (UIButton*)[cell.contentView viewWithTag:4000];
[marketButton setTag:indexPath.row];
if([sellingArray count]>0)
{
NSLog(#"sellingArray %#",sellingArray);
if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"0"]) // nothing
{
[marketButton setSelected:NO];
[marketButton setImage:[UIImage imageNamed:#"Marketplace.png"] forState:UIControlStateNormal];
marketButton.enabled = YES;
}
else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"2"]) // marketplace
{
[marketButton setSelected:YES];
[marketButton setImage:[UIImage imageNamed:#"MarketplaceSelect.png"] forState:UIControlStateNormal];
marketButton.enabled = YES;
}
}
}
UILabel *pricelbl = (UILabel*)[cell.contentView viewWithTag:3000];
pricelbl.text =[NSString stringWithFormat:#"$%#",[priceNewArray objectAtIndex:indexPath.row]];
if ([sellingArray count]>0) {
if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"2"]){
pricelbl.hidden = NO;
}
else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"0"]){
pricelbl.hidden = YES;
}
}
return cell;
}
marketpressAction :
- (void)marketPressedAction:(id)sender
{
buttonPressed = (UIButton *)sender;
buttontag = buttonPressed.tag;
NSLog(#"Market button click at row %d",buttontag);
}

Resources