I want to create a check box in each row in in tableview. My code works for only one checkbox. If i have more than 1 it fails. My code is
UIButton * checkBox=[UIButton buttonWithType:UIButtonTypeCustom];
checkBox.tag=indexPath.row;
checkBox.frame=CGRectMake(270,15, 20, 20);
[cell.contentView addSubview:checkBox];
[checkBox setImage:[UIImage imageNamed:#"selectedBoxWith.png"] forState:UIControlStateNormal];
[checkBox addTarget:self action:#selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];
-(void)checkBoxClicked:(id)sender{
if(self.isChecked ==NO){
self.isChecked =YES;
[sender setImage:[UIImage imageNamed: #"selectedBoxWithTik.png"] forState:UIControlStateNormal];
}else
{
self.isChecked =NO;
[sender setImage:[UIImage imageNamed:#"selectedBoxWith.png"]forState:UIControlStateNormal];
}
}
How can I handle it for more checkbox?
You just need to try this. It will work..........
-(void)checkBoxClicked:(id)sender{
UIButton *tappedButton = (UIButton*)sender;
if([tappedButton.currentImage isEqual:[UIImage imageNamed:#"selectedBoxWith.png"]]) {
[sender setImage:[UIImage imageNamed: #"selectedBoxWithTik.png"] forState:UIControlStateNormal];
}
else {
[sender setImage:[UIImage imageNamed:#"selectedBoxWith.png"]forState:UIControlStateNormal];
}
}
You can not simply do this by using a simple UIButton as you will be needing to detect which row number checkbox was pressed. Hence instead of using a UIButton directly use a CustomButton which extends UIButton and has an extra property rowTag like:
#interface CustomButton : UIButton {
NSInteger rowTag;
}
#property NSInteger rowTag;
#end
#implementation CustomButton
#synthesize rowTag;
#end
Now in cellForRowAtIndexPath:
if(cell==nil){
CustomButton * checkBox=[CustomButton buttonWithType:UIButtonTypeCustom];
checkBox.tag=21;
checkBox.frame=CGRectMake(270,15, 20, 20);
[cell.contentView addSubview:checkBox];
[checkBox addTarget:self action:#selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];
}
CustomButton *btn = [cell.contentView viewWithTag:21];
[btn setRowTag:indexPath.row];
if(selectedProperty){
[checkBox setImage:[UIImage imageNamed:#"selectedBoxWithTik.png"] forState:UIControlStateNormal];
}
else
[checkBox setImage:[UIImage imageNamed:#"selectedBoxWith.png"] forState:UIControlStateNormal];
Hope this helps.
Example, click on first row is used to change for other rows. Click on other rows only change itself. arrCheckbox is an array used to store check of all rows.
- (UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//NSLog(#"AV--%s", __func__);
HistoryCell2 *cell = [tableview dequeueReusableCellWithIdentifier:CellId];
if (cell == nil) {
NSLog(#"Create new row");
[tableview registerNib:[UINib nibWithNibName:#"HistoryCell2" bundle:nil] forCellReuseIdentifier:CellId];
cell = [tableview dequeueReusableCellWithIdentifier:CellId];
}
cell.tag = indexPath.row;
BOOL checked = [[arrCheckbox objectAtIndex:indexPath.row] boolValue];
UIImage *image = (checked) ? [UIImage imageNamed:#"cb_on.png"] : [UIImage imageNamed:#"cb_off.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(1.0, 1.0, 29, 29);
button.frame = frame; // match the button's size with the image size
button.tag = indexPath.row;
[button setBackgroundImage:image forState:UIControlStateNormal];
// set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
[button addTarget:self action:#selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
return cell;
}
- (void) checkButtonTapped:(id)sender
{
NSLog(#"%s", __func__);
UIButton *tappedButton = (UIButton*)sender;
int index = (int)tappedButton.tag;
NSLog(#"Row button: %d", index);
if(index > 0)
{
BOOL checked = [[arrCheckbox objectAtIndex:index] boolValue];
[arrCheckbox removeObjectAtIndex:index];
[arrCheckbox insertObject:(checked) ? #"0":#"1" atIndex:index];
UIImage *newImage = (checked) ? [UIImage imageNamed:#"cb_off.png"] : [UIImage imageNamed:#"cb_on.png"];
[tappedButton setBackgroundImage:newImage forState:UIControlStateNormal];
}
else{
//UITableView *tableview = (UITableView *)[[tappedButton superview] superview];
UIImage *newImage;
BOOL checked = [[arrCheckbox objectAtIndex:0] boolValue];
for(int i=0; i<[arrCheckbox count]; i++)
{
//NSLog(#"Row: %d------", i);
[arrCheckbox removeObjectAtIndex:i];
[arrCheckbox insertObject:(checked) ? #"0":#"1" atIndex:i];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
for(UIView *subview in cell.contentView.subviews)
{
if([subview isKindOfClass: [UIButton class]])
{
//NSLog(#"Modify button");
tappedButton = (UIButton*)subview;
//[subview removeFromSuperview];
newImage = (checked) ? [UIImage imageNamed:#"cb_off.png"] : [UIImage imageNamed:#"cb_on.png"];
[tappedButton setBackgroundImage:newImage forState:UIControlStateNormal];
}
}
}
}
}
Related
I have a UITableViewCell in which I'm creating some buttons (which are acting like radiobuttons) and assigning tags to the buttons. But the tags are not static. They are dynamic. Now when a particular radiobutton is clicked I need to set that button with some image (radio_On.png) and remaining all buttons as (radio_Off.png). But to set the images I couldn't understand how to get the tag values of all buttons in that particular cell because they are not static.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
btTemp1 = [[UIButton alloc]initWithFrame:CGRectMake(10, lblQuestion.frame.origin.y+lblQuestion.frame.size.height+3,17, 17)];
[btTemp1 addTarget:self action:#selector(radioButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
btTemp1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[btTemp1 setImage:[UIImage imageNamed:#"radio_button_off.png"] forState:UIControlStateNormal];
[btTemp1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btTemp1.titleLabel.font =[UIFont systemFontOfSize:14.f];
btTemp1.tag=++Optionid;
[hlcell.contentView addSubview:btTemp1];
btTemp2 = [[UIButton alloc]initWithFrame:CGRectMake(10, lblOption1.frame.origin.y+lblOption1.frame.size.height,17, 17)];
[btTemp2 addTarget:self action:#selector(radioButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
btTemp2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[btTemp2 setImage:[UIImage imageNamed:#"radio_button_off.png"] forState:UIControlStateNormal];
[btTemp2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btTemp2.titleLabel.font =[UIFont systemFontOfSize:14.f];
btTemp2.tag=++Optionid;
[hlcell.contentView addSubview:btTemp2];
}
-(IBAction) radioButtonClicked:(UIButton *) sender {
UIButton *button = (UIButton *)sender;
NSLog(#"%d", [button tag]);
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)
[[sender superview] superview]];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
NSMutableArray *cellSection = [self.finalarray objectAtIndex:indexPath.section];
[sender setImage:[UIImage imageNamed:#"radio-On.png"] forState:UIControlStateNormal];
}
Here one radiobuttton is not getting released when I check other button in TableView cell.
EDIT:
Prevoiusly I gave this code:
-(IBAction) radioButtonClicked:(UIButton *) sender {
UIButton *button = (UIButton *)sender;
NSLog(#"%d", [button tag]);
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)
[[sender superview] superview]];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
NSMutableArray *cellSection = [self.finalarray objectAtIndex:indexPath.section];
UIButton *btnTemp1 = (UIButton*)[cell viewWithTag:10]; //Static .But now I want dynamic
UIButton *btnTemp2 = (UIButton*)[cell viewWithTag:11];//Static .But now I want dynamic
UIButton *btnTemp3 = (UIButton*)[cell viewWithTag:12];//Static .But now I want dynamic
UIButton *btnTemp4=(UIButton *)[cell viewWithTag:13];//Static .But now I want dynamic
UIButton *btnTemp5=(UIButton *)[cell viewWithTag:14];//Static .But now I want dynamic
[radioButtonsinaSection addObject:btnTemp1];
[radioButtonsinaSection addObject:btnTemp2];
[radioButtonsinaSection addObject:btnTemp3];
[radioButtonsinaSection addObject:btnTemp4];
[radioButtonsinaSection addObject:btnTemp5];
}
for(int i=0;i<[radioButtonsinaSection count];i++){
[[radioButtonsinaSection objectAtIndex:i] setImage:[UIImage imageNamed:#"radio_button_off.png"] forState:UIControlStateNormal];
}
[sender setImage:[UIImage imageNamed:#"radio-On.png"] forState:UIControlStateNormal];
}
How can I get the all the controls (buttons and their tags) of a cell in which button (radiobutton) is clicked ?
As per my knowledge you miss the loop in radioButtonClicked method,which keeps track of which button is clicked..you try this code..
(IBAction) radioButtonClicked:(UIButton *) sender{
for(int i=0;i<[self.radioButtons count];i++){
[[self.radioButtons objectAtIndex:i] setImage:[UIImage
imageNamed:#"radio-off.png"]
forState:UIControlStateNormal];
}
[sender setImage:[UIImage imageNamed:#"radio-On.png"]
forState:UIControlStateNormal];
}
I think it helps you,if not feel free to ask again..
Enjoy..
So I'm trying to build here some kind of a to-do list - I have a table view, and on each cell I have a thumbnail which represents a checkbox. On touch, I want it to change images - from full to empty and vice versa. What I'm doing wrong? Here's some code :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"taskCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 32, 32)];
[button addTarget:self action:#selector(checkmarkPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:#"checkbox_empty"] forState:UIControlStateNormal];
[button setTag:101];
[cell addSubview:button];
[cell setIndentationWidth:36];
[cell setIndentationLevel:1];
cell.textLabel.text = [[self.appDel.allTasks objectAtIndex:indexPath.row] taskName];
return cell;
}
- (void) checkmarkPressed:(UIButton *)sender{
if(sender.tag == 101){
UITableViewCell *cell = ((UITableViewCell*)[sender superview]);
NSLog(#"Cell %i", [self.tableView indexPathForCell:cell].row);
UIImage *empty = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"checkbox_empty" ofType:#"png"]];
UIImage *full = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"checkbox_full" ofType:#"png"]];
if ([sender imageView].image == [UIImage imageNamed:#"checkbox_empty"]) {
[sender setImage:full forState:UIControlStateNormal];
[self.tableView reloadData];
}
if ([sender imageView].image == full) {
[sender setImage:empty forState:UIControlStateNormal];
[self.tableView reloadData];
}
}
}
If you just want to change the image on touch , you can try this code
- (void) checkmarkPressed:(UIButton *)tempButton{
if (tempButton.tag == 101) {
tempButton.tag = 104;
[sender setImage:full forState:UIControlStateNormal];
}
else if (tempButton.tag == 104) {
tempButton.tag = 101;
[sender setImage:empty forState:UIControlStateNormal];
}
[self.tableView reloadData];
}
Found the solution :
if ([sender imageView].image == [UIImage imageNamed:#"checkbox_empty"]) {
[sender removeFromSuperview];
[sender setImage:full forState:UIControlStateNormal];
[[sender superview] addSubview:sender];
[self.tableView reloadData];
}
else if ([sender imageView].image == [UIImage imageNamed:#"checkbox_full"]) {
[sender removeFromSuperview];
[sender setImage:empty forState:UIControlStateNormal];
[[sender superview] addSubview:sender];
[self.tableView reloadData];
}
I have created a custom cell like
In this "+" sign increases the ticket count and "-" decreases it. When ticket count is 0, "-" is disabled and when ticket count is 4 "+" is disabled. It is working fine till now. The problem is Now i want to change the frame size of table when I click the "+" button of 4th row and regain the Frame size when I click "-" of 4th row. When i do so "-" functionality of other row changes randomly.PLease let me know what am i doing wrong.Thanks Here is my code
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier=#"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil)
{
cell
= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease];
UILabel *labelOne = [[UI
Label alloc]initWithFrame:CGRectMake(70, 10, 100, 20)];
labelOne.tag = 100;
labelOne.backgroundColor=[UIColor clearColor];
labelOne.font=[UIFont systemFontOfSize:14];
[cell.contentView addSubview:labelOne];
[labelOne release];
minusbutton=[[UIButton alloc]initWithFrame:CGRectMake(152, 5, 15, 20)];
minusbutton.tag=104;
[minusbutton setEnabled:NO];
minusbutton.backgroundColor=[UIColor clearColor];
[minusbutton addTarget:self action:#selector(DecreaseTickets:) forControlEvents:UIControlEventTouchUpInside];
[minusbutton setTitle:#"-" forState:UIControlStateNormal];
[minusbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[minusbutton setBackgroundImage:[UIImage imageNamed:#"button-green1.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:minusbutton];
[minusbutton release];
UILabel *quantity = [[UILabel alloc]initWithFrame:CGRectMake(170, 5, 20, 20)];
quantity.tag = 103;
quantity.backgroundColor=[UIColor clearColor];
quantity.font=[UIFont systemFontOfSize:12];
[cell.contentView addSubview:quantity];
[quantity release];
addbutton=[[UIButton alloc]initWithFrame:CGRectMake(192, 5, 15, 20)];
addbutton.tag=105;
addbutton.backgroundColor=[UIColor clearColor];
[addbutton addTarget:self action:#selector(IncreaseTickets:) forControlEvents:UIControlEventTouchUpInside];
[addbutton setTitle:#"+" forState:UIControlStateNormal];
[addbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[addbutton setBackgroundImage:[UIImage imageNamed:#"button-green1.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:addbutton];
[addbutton release];
UILabel *labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(240, 10, 80, 20)];
labelTwo.tag = 101;
labelTwo.backgroundColor=[UIColor clearColor];
labelTwo.font=[UIFont systemFontOfSize:14];
labelTwo.textColor=[UIColor colorWithRed:0.25098 green:0.447059 blue:0.07451 alpha:1];
[cell.contentView addSubview:labelTwo];
[labelTwo release];
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(70, 30, 230, 30)];
label3.tag = 102;
label3.backgroundColor=[UIColor clearColor];
label3.numberOfLines=2;
label3.font=[UIFont systemFontOfSize:12];
[cell.contentView addSubview:label3];
[label3 release];
}
}
UILabel *labelOne = (UILabel *) [cell.contentView viewWithTag:100];
NSString *ss2=[[NSString alloc]initWithFormat:#"%#",[[eventTicketListArray objectAtIndex:indexPath.row ]tit] ];
labelOne.text = ss2;
UILabel *labelTwo = (UILabel *) [cell.contentView viewWithTag:101];
NSString *string2=[[NSString alloc]initWithFormat:#"%#",[[eventTicketListArray objectAtIndex:indexPath.row ]price] ];
labelTwo.text =string2;
UILabel *label3 = (UILabel *) [cell.contentView viewWithTag:102];
label3.text=[[NSString alloc]initWithFormat:#"%#",[[eventTicketListArray objectAtIndex:indexPath.row ]desc ] ];
UILabel *label4 = (UILabel *) [cell.contentView viewWithTag:103];
label4.text=[[NSString alloc]initWithFormat:#"%d",[[eventTicketListArray objectAtIndex:indexPath.row ]qty ] ];
}
return cell;
}
Method for + button click
-(void)IncreaseTickets:(id)sender{
NSIndexPath *indexPath =[ticketTable indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
NSUInteger row = indexPath.row;
UITableViewCell *currentCell = (UITableViewCell *)[[sender superview] superview] ;
for(UILabel *lbl in [currentCell.contentView subviews])
{
if(([lbl isKindOfClass:[UILabel class]]) && ([lbl tag] == 103))
{
str = [lbl.text intValue];
str++;
eventTicketList=(EventDetailTicketsList *)[eventTicketListArray objectAtIndex:row];
eventTicketList.qty=str;
lbl.text=[NSString stringWithFormat:#"%d",str];
}
}
for(UIButton *btn in [currentCell.contentView subviews])
{
if(([btn isKindOfClass:[UIButton class]]) && ([btn tag] == 105))
{
if (str==4) {
[btn setEnabled:NO];
}
}
}
for(UIButton *btn in [currentCell.contentView subviews])
{
if(([btn isKindOfClass:[UIButton class]]) && ([btn tag] == 104))
{
[btn setEnabled:YES];
}
}
if (row==3) {
ticketTable.frame=CGRectMake(0, 0, 320, 70);
}
[currentCell setNeedsDisplay];
}
Method for - button click
-(void)DecreaseTickets:(id)sender{
NSIndexPath *indexPath =[ticketTable indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
NSUInteger row = indexPath.row;
UITableViewCell *currentCell = (UITableViewCell *)[[sender superview] superview] ;
for(UILabel *lbl in [currentCell.contentView subviews])
{
if(([lbl isKindOfClass:[UILabel class]]) && ([lbl tag] == 103))
{
str = [lbl.text intValue];
str--;
eventTicketList=(EventDetailTicketsList *)[eventTicketListArray objectAtIndex:row];
eventTicketList.qty=str;
lbl.text=[NSString stringWithFormat:#"%d",str];
}
}
for(UIButton *btn in [currentCell.contentView subviews])
{
if(([btn isKindOfClass:[UIButton class]]) && ([btn tag] == 104))
{
if (str<=0) {
[btn setEnabled:NO];
}
}
}
for(UIButton *btn in [currentCell.contentView subviews])
{
if(([btn isKindOfClass:[UIButton class]]) && ([btn tag] == 105))
{
[btn setEnabled:YES];
}
}
if (row==3){
ticketTable.frame=CGRectMake(0, 0, 320, 200);
}
[currentCell setNeedsDisplay];
}
Add below Code in you button click or you can also use this method in didSelectRowForIndexPath.
[self.tableView beginUpdates];
[self.tableView endUpdates];
Use this will automatically handle tableview call Below Method.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 300;// here you can calculate cell height according to your requirements.
}
Hope, this will help you...
I have a tableview where I am displaying my values from database in tableview cells. In this tableview cellforrowatindexpath, I have created a button with an image on it and set selector for it. So when I click on the button my selector is called and it changes my image to another image.
But the problem is it does not identifies the indexpath i.e. if 4 rows are present in the tableview and if I click on the first row button, its image should change but problem is 4th row action is performed and its image is changed because it does not get the proper indexpath of the image to change.
This is my cellforrowatindexpath code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell%d%d", indexPath.section, indexPath.row];
appDelegate = (StopSnoozeAppDelegate*)[[UIApplication sharedApplication]delegate];
cell =(TAlarmCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TAlarmCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
mimageButton = [UIButton buttonWithType:UIButtonTypeCustom];
mimageButton.frame=CGRectMake(10, 20, 20, 20);
mimageButton.tag = 1;
onButtonView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 50)];
onButtonView.tag = 2;
onButtonView.image = [UIImage imageNamed:#"alarm_ON.png"];
[mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
[cell.contentView addSubview:mimageButton];
[mimageButton addTarget:self action:#selector(changeMapType::) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
This is my changemapType code
-(void)changeMapType:(NSIndexPath*)path1:(UIButton*)sender{
appDelegate.changeimagetype =!appDelegate.changeimagetype;
if(appDelegate.changeimagetype == YES)
{
onButtonView.image = [UIImage imageNamed:#"alarm_OF.png"];
[mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
appDelegate.changeimagetype = YES;
}
else
{
onButtonView.image = [UIImage imageNamed:#"alarm_ON.png"];
[mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
appDelegate.changeimagetype = NO;
}
}
Don't break table view cell reuse just to put a different tag on each button. If your cells are the same, use the same reuse identifier.
You can find out the index path of the sender like this, without any need to mess around with tags. It also works for multi section tables.
CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];
I don't think you can have additional arguments in an #selector like that. What you might have to do is subclass UIButton and add an NSIndexPath property (or even just an int) and in -(void)changeMapType:(MyCustomButton*)sender access the property there.
Also it seems you do not even use the index path in changeMapType so that will need to be changed as well.
EDIT: Is it the button image you are trying to change? In which case you don't need the index path at all, or a subclass. Just use [sender setImage:(UIImage *)image forState:(UIControlState)state].
Use yourTableView for getting the Index Path.. Try something like this.
- (void)buttonAction:(UIButton*)sender {
UITableViewCell *cell = (UITableViewCell*)button.superview;
NSIndexPath *indexPath = [yourTableView indexPathForCell:cell];
NSLog(#"%d",indexPath.row)
}
I think following code might help you..
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIView *myCheckbox = [[UIView alloc] init];
UIButton *myBt1 = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
if([appDelegate.changeimagetype == YES"]){
[myBt1 setBackgroundImage:[UIImage imageNamed:#"alarm_ON.png"] forState:UIControlStateNormal];
}
else {
[myBt1 setBackgroundImage:[UIImage imageNamed:#"alarm_OF.png"] forState:UIControlStateNormal];
}
[myBt1 addTarget:self action:#selector(btcheckbox:) forControlEvents:UIControlEventTouchDown];
[myBt1 setTag:indexPath.row];
[myCheckbox addSubview:myBt1];
[myBt1 release];
[myCheckbox release];
[cell addsubview:myview];
return cell;
}
-(void) btcheckbox:(id) sender
{
UIButton *currentButton = (UIButton *) sender;
if([[currentButton backgroundImageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:#"alarm_OF.png"]])
{
appDelegate.changeimagetype = YES;
[currentButton setBackgroundImage:[UIImage imageNamed:#"alarm_ON.png"] forState:UIControlStateNormal];
}else if([[currentButton backgroundImageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:#"alarm_ON.png"]])
{
appDelegate.changeimagetype = NO;
[currentButton setBackgroundImage:[UIImage imageNamed:#"alarm_OF.png"] forState:UIControlStateNormal];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell%d", indexPath.row];
appDelegate = (StopSnoozeAppDelegate*)[[UIApplication sharedApplication]delegate];
cell =(TAlarmCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TAlarmCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIButton *mimageButton = [UIButton buttonWithType:UIButtonTypeCustom];
mimageButton.frame=CGRectMake(10, 20, 20, 20);
mimageButton.tag = indexPath.row;
//onButtonView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 50)];
//onButtonView.tag = 2;
// onButtonView.image = [UIImage imageNamed:#"alarm_ON.png"];
[mimageButton setImage:[UIImage imageNamed:#"alarm_ON.png"] forState:UIControlStateNormal];
mimageButton.selected = NO;
[cell.contentView addSubview:mimageButton];
[mimageButton addTarget:self action:#selector(changeMapType:) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
-(void)changeMapType:(UIButton*)sender{
if(sender.selected == YES)
{
// onButtonView.image = [UIImage imageNamed:#"alarm_OF.png"];
[sender setImage:[UIImage imageNamed:#"alarm_ON.png"] forState:UIControlStateNormal];
sender.selected = NO;
}
else
{
//onButtonView.image = [UIImage imageNamed:#"alarm_ON.png"];
[sender setImage:[UIImage imageNamed:#"alarm_OF.png"] forState:UIControlStateNormal];
sender.selected = YES;
}
}
I want to change item-Button value on Plus and Minus Button click of tableviewcell.
This is my tableview cell for row index method code.
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [menuTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
UIButton *minusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
minusBtn.frame= CGRectMake(400, 5,30,30);
minusBtn.tag = indexPath.row;
[minusBtn setImage:[UIImage imageNamed:#"removeButan.png"] forState:UIControlStateNormal];
[minusBtn addTarget:self action:#selector(minusAction:) forControlEvents:UIControlEventTouchUpInside];
UIButton *plusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
plusBtn.frame= CGRectMake(500, 5,30,30);
plusBtn.tag = indexPath.row;
[plusBtn setImage:[UIImage imageNamed:#"Addbutton.png"] forState:UIControlStateNormal];
[plusBtn addTarget:self action:#selector(plusAction:) forControlEvents:UIControlEventTouchUpInside];
UIButton *itemBtn = [UIButton buttonWithType:UIButtonTypeCustom];
itemBtn.frame= CGRectMake(450, 5,30,30);
itemBtn.tag = indexPath.row;
[itemBtn setBackgroundImage:[UIImage imageNamed:#"number_butan_1.png"] forState:UIControlStateNormal];
[itemBtn setTitle:totalItem forState:UIControlStateNormal];
[itemBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
UIButton *addToCart = [UIButton buttonWithType:UIButtonTypeCustom];
addToCart.frame= CGRectMake(580,5,30,30);
addToCart.tag = indexPath.row;
addToCart.font = [UIFont systemFontOfSize:14];
[addToCart setImage:[UIImage imageNamed:#"cart.png"] forState:UIControlStateNormal];
[addToCart addTarget:self action:#selector(addToCartAction:) forControlEvents:UIControlEventTouchUpInside];
UIButton *infoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
infoBtn.frame= CGRectMake(630, 5,30,30);
infoBtn.tag = indexPath.row;
[infoBtn setImage:[UIImage imageNamed:#"info.png"] forState:UIControlStateNormal];
[infoBtn addTarget:self action:#selector(displayDetailView:) forControlEvents:UIControlEventTouchUpInside];
// lblTitle.text = [nameArray objectAtIndex:indexPath.row];
cell.primaryLabel.text = [appDelegate.nameArray objectAtIndex:indexPath.row];
cell.secondaryLabel.text = [appDelegate.sortDetailArray objectAtIndex:indexPath.row];
cell.priceLabel.text = [appDelegate.rateArray objectAtIndex:indexPath.row];
[cell.contentView addSubview:minusBtn];
[cell.contentView addSubview:plusBtn];
[cell.contentView addSubview:addToCart];
[cell.contentView addSubview:itemBtn];
[cell.contentView addSubview:infoBtn];
return cell;
}
- (IBAction)plusAction:(id)sender{
// Hat* obj = ((UIButton *)sender).tag;
// NSNumber* hatId = obj.idValue;
UITableViewCell *cell=(UITableViewCell*)[[sender superview] superview];
UITableView *table=(UITableView*)[cell superview];
NSIndexPath *path=[table indexPathForCell:cell];
int value = [totalItem intValue];
value++;
totalItem = [NSString stringWithFormat:#"%d",value];
NSLog([NSString stringWithFormat:#"%#",totalItem]);
[menuTableView reloadData];
}
- (IBAction)minusAction:(id)sender{
UIView *contentView = [sender superview];
UITableViewCell *cell=(UITableViewCell*)[[contentView superview] superview];
UITableView *table=(UITableView*)[cell superview];
NSIndexPath *path=[table indexPathForCell:cell];
NSUInteger buttonRow = [[menuTableView
indexPathForCell:cell] row];
int value = [totalItem intValue];
value--;
totalItem = [NSString stringWithFormat:#"%d",value];
NSLog([NSString stringWithFormat:#"%#",totalItem]);
[menuTableView reloadData];
}
i am trying this but not working.it change all row value
You only have one instance of the totalItem. If you have many rows and you want to store each one individually, you will need to create an array to store each of the individual rows and then display the appropriate value in the cellForRow method.
I've done it.
- (IBAction)plusAction:(id)sender{
int correctIndex = [sender tag];
int old = [[appDelegate.quantityArray objectAtIndex:correctIndex] intValue];
[appDelegate.quantityArray replaceObjectAtIndex:correctIndex withObject:[NSNumber numberWithInt:old + 1]];
[menuTableView reloadData];
}
- (IBAction)minusAction:(id)sender{
int correctIndex = [sender tag];
int old = [[appDelegate.quantityArray objectAtIndex:correctIndex] intValue];
[appDelegate.quantityArray replaceObjectAtIndex:correctIndex withObject:[NSNumber numberWithInt:old - 1]];
[menuTableView reloadData];
}