Checkboxes icons are not shown properly - ios

i am making an app in which i am using tableview cell and in uitableview cell i am using button in the form of checkbox. i have two images named "uncheck.png" and "tick.png" when i click on that button tick is shown but uncheck image is disappear i want that tick image on that uncheck image...!
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *tableviewidentifier = #"cell";
tablecellTableViewCell *cell= [self.activitiesTableView_ dequeueReusableCellWithIdentifier:tableviewidentifier];
if(cell==nil)
{
cell = [[tablecellTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tableviewidentifier];
}
UILabel *valuedate = (UILabel *)[cell viewWithTag:11];
UILabel *msg = (UILabel *)[cell viewWithTag:12];
UILabel *date = (UILabel *)[cell viewWithTag:13];
UILabel *time = (UILabel *)[cell viewWithTag:14];
[valuedate setText:#"Demo"];
[msg setText:#"How are You?"];
date.text=#"14/07/2014";
time.text=#"A 08:16";
// [cell.textLabel setText:activityModel.userName];
valuedate.font=[UIFont fontWithName:#"SegoeUI" size:15];
msg.font=[UIFont fontWithName:#"SegoeUI-light" size:10.0];
date.font=[UIFont fontWithName:#"SegoeUI-light" size:9];
time.font=[UIFont fontWithName:#"SegoeUI-light" size:9];
if ([self.checkimageArray containsObject:[self.lblArray objectAtIndex:indexPath.row]])
{
[cell.button setImage:[UIImage imageNamed:#"tick.png"] forState:UIControlStateNormal];
//[cell.button setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
}
else
{
[cell.button setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
}
cell.button.tag=indexPath.row;
[cell.button addTarget:self action:#selector(checkButton:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}

Use your uncheck image as background image using
[cell.button setBackgroundImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
and then your check image as image
[cell.button setImage:[UIImage imageNamed:#"tick.png"] forState:UIControlStateNormal];
Now you can change the image of button at each tap but the background image will be fixed. when the button is selected set the tick image as image else set it as nil
Replace your code with this
[cell.button setBackgroundImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
[cell.button setImage:nil forState:UIControlStateNormal];
if ([self.checkimageArray containsObject:[self.lblArray objectAtIndex:indexPath.row]])
{
[cell.button setImage:[UIImage imageNamed:#"tick.png"] forState:UIControlStateNormal];
}
May be this can solve your problem

Related

viewWithTag UIButton not working on tableview reloadData

I'm building a survey app with a table view displaying the questions in rows. Some rows have text fields, some have sliders and some have 5 buttons for the user to select their answers.
I can set the initial UIButton values just fine, with multiple cells of buttons and each button can have their titles set. But when I change the data model and reload the tableview, the first row (0) works but the second row (1) does not. The button1.tag returns 0 for the second row.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell forIndexPath:indexPath];
NSString *subCatKey = [NSString stringWithFormat:#"%ld", indexPath.row];
NSDictionary *thisSubCat = [_subCatListDict objectForKey:subCatKey];
NSString *defaultResponse = #"";
static NSString *CellIdentifier;
CellIdentifier = #"buttonCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIButton *button1 = [[UIButton alloc] init];
UIButton *button2 = [[UIButton alloc] init];
UIButton *button3 = [[UIButton alloc] init];
UIButton *button4 = [[UIButton alloc] init];
UIButton *button5 = [[UIButton alloc] init];
NSLog(#"Button 1::%ld",(long)button1.tag);
// - 4 = Button range
NSLog(#"Setting up the button cell");
NSArray *buttonResponses = [[NSArray alloc] initWithArray:[thisSubCat objectForKey:#"responses"]];
[buttonResponses objectAtIndex:0];
if(cell == nil )
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSLog(#"Button tag 1::%ld",(long)button1.tag);
if (button1.tag == 0) {
NSLog(#"Setting buttons...");
button1 = (UIButton *)[cell viewWithTag:41];
NSLog(#"Button tag 1::%ld",(long)button1.tag);
button2 = (UIButton *)[cell viewWithTag:42];
button3 = (UIButton *)[cell viewWithTag:43];
button4 = (UIButton *)[cell viewWithTag:44];
button5 = (UIButton *)[cell viewWithTag:45];
[button1 setTitle:[buttonResponses objectAtIndex:0] forState:UIControlStateNormal];
[button2 setTitle:[buttonResponses objectAtIndex:1] forState:UIControlStateNormal];
[button3 setTitle:[buttonResponses objectAtIndex:2] forState:UIControlStateNormal];
[button4 setTitle:[buttonResponses objectAtIndex:3] forState:UIControlStateNormal];
[button5 setTitle:[buttonResponses objectAtIndex:4] forState:UIControlStateNormal];
[button1 addTarget:self
action:#selector(rowButtonSelect:)
forControlEvents:UIControlEventTouchUpInside];
[button2 addTarget:self
action:#selector(rowButtonSelect:)
forControlEvents:UIControlEventTouchUpInside];
[button3 addTarget:self
action:#selector(rowButtonSelect:)
forControlEvents:UIControlEventTouchUpInside];
[button4 addTarget:self
action:#selector(rowButtonSelect:)
forControlEvents:UIControlEventTouchUpInside];
[button5 addTarget:self
action:#selector(rowButtonSelect:)
forControlEvents:UIControlEventTouchUpInside];
}
UILabel *questionLabel = (UILabel *)[cell viewWithTag:301];
questionLabel.text = [thisSubCat objectForKey:#"question"];
UILabel *questionResponses = (UILabel *)[cell viewWithTag:302];
questionResponses.text = defaultResponse;
NSLog(#"Default response:%#",defaultResponse);
return cell;
}
You always Create new Button in cell so it's default tag = 0 so
NSLog(#"Button tag 1::%ld",(long)button1.tag);
will print Button tag 1::0
set
button1.tag indexpath.raw;
and try

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 show count of checkboxes botton in a label

i am making an app in which i am using checkboxes in uitableview cell. Now i am stuck on it how can i show count of selected checkboxes in a label. like i check one box then in label "1 checkbox selected" will shown and if i select 2 check boxes then in label "2 checkbox selected" will shown how it can be done any idea? below is my code where i am handling my checkbox button:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *tableviewidentifier = #"cell";
tablecellTableViewCell *cell= [self.activitiesTableView_ dequeueReusableCellWithIdentifier:tableviewidentifier];
if(cell==nil)
{
cell = [[tablecellTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tableviewidentifier];
}
UILabel *valuedate = (UILabel *)[cell viewWithTag:11];
UILabel *msg = (UILabel *)[cell viewWithTag:12];
UILabel *date = (UILabel *)[cell viewWithTag:13];
UILabel *time = (UILabel *)[cell viewWithTag:14];
[valuedate setText:#"Demo"];
[msg setText:#"How are You?"];
date.text=#"14/07/2014";
time.text=#"A 08:16";
// [cell.textLabel setText:activityModel.userName];
valuedate.font=[UIFont fontWithName:#"SegoeUI" size:15];
msg.font=[UIFont fontWithName:#"SegoeUI-light" size:10.0];
date.font=[UIFont fontWithName:#"SegoeUI-light" size:9];
time.font=[UIFont fontWithName:#"SegoeUI-light" size:9];
if ([self.checkimageArray containsObject:[self.lblArray objectAtIndex:indexPath.row]])
{
[cell.button setImage:[UIImage imageNamed:#"tick.png"] forState:UIControlStateNormal];
//[cell.button setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
}
else
{
[cell.button setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
}
cell.button.tag=indexPath.row;
[cell.button addTarget:self action:#selector(checkButton:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
viewDidLoad
yourTableView.allowsmultiselection=YES;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
[cell.btn setBackgroundImage:[UIImage imageNamed:#"selected-24.png"] forState:UIControlStateNormal];
NSArray *arr=[tableView indexPathsForSelectedRows];
lbl.textLabel.text=[NSString stringWithFormat:#"%d",[arr count] ];
}
in deselect
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
[cell.btn setBackgroundImage:[UIImage imageNamed:#"unselected-24.png"] forState:UIControlStateNormal];
NSArray *arr=[tableView indexPathsForSelectedRows];
lbl.textLabel.text=[NSString stringWithFormat:#"%d",[arr count] ];
}
Take a NSMutableArray for selelcted buttons.when you select particuler button at that time add that indexpath.row value to the array.And when you delselect the button at that time check wheateher that indexpath.row value present in that array or not ,if yes then find the index of that object and remove that object from the array.
if([array containsObject:#"value"]){
int n=[array indexOfObject:#"value"];
[array removeObjectAtIndex:n];
}
Now check the count of array as [array count];

UIButton of UITableViewCell disappear on scroll

I Have a problem with my custom UItableViewCell. It's seems that all UIButton of UITableViewCell disappear on scroll. Specilay on my 25 cell. Any one have any idea ?
any help would be appreciated :)
That some code :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString* cellIdentifier = #"Cell";
Cell * __strong cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell){
cell = [[Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
InfoObject __weak *obj = [_pos objectAtIndex:indexPath.row];
[cell configCell];
cell.id = obj.id;
{// EVENT ON BUTTON
[cell.checkbox addTarget:self action:#selector(checkup:) forControlEvents:UIControlEventTouchUpInside];
[cell.btnPaymentState addTarget:self action:#selector(changePaymentState:) forControlEvents:UIControlEventTouchUpInside];
}
{// SET BUTTON CHECKBOX
cell.checkbox.tag = indexPath.row;
[cell.checkbox setImage:[UIImage imageNamed:IMG_BLACK_UNCHECKBOX] forState:UIControlStateNormal];
[cell.checkbox setImage:[UIImage imageNamed:IMG_BLACK_CHECKBOX] forState:UIControlStateSelected];
[cell.checkbox setImage:[UIImage imageNamed:IMG_BLACK_CHECKBOX] forState:UIControlStateHighlighted];
}
{// SET BUTTON PAYMENT STATE
cell.btnPaymentState.tag = indexPath.row;
[cell.btnPaymentState setImage:[self getImageAssociateWithName:obj.stringPaymentState] forState:UIControlStateNormal];
[cell.btnPaymentState setImage:[self getImageAssociateWithName:obj.stringPaymentState] forState:UIControlStateSelected];
[cell.btnPaymentState setImage:[self getImageAssociateWithName:obj.stringPaymentState] forState:UIControlStateHighlighted];
}
[self SetSelectedBackgroundColorSelectedForFolderCellAtRow:indexPath.row
:obj.selected Cell: cell];
[cell.checkbox setSelected:obj.selected];
[self setCellColorTextOfObjectPrepare:cell withObj:obj];
return cell;
}
I solve my problem. The problem is that i was using Tag for my UIButton and the identifier was the same number tag of another UIView so when i was removing my UIView the UIButton disappear also. Thanks all for your answer.
Remove if (!cell)
And you're good to go.

How to disable UIButton in tableview after called another UIVIew in 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;
}

Resources