I am using a CoreData in my project. In my project one TableViewCell and one add button. add button click and move to another view and and some text fields add in another view. then textfield text save proper in database by CoreData. and then proper fetch values and reload on TableViewCell.
My question is that TableViewCell on one button. This button are use check and uncheck cells. I want to button is select and update value on database. I want a same entity value are update. and same index value are update.
Suppose click on button and set the right sign image and save value in database same table and same value. and again click on button and set the blank box image.
Now button selection on change images but do not save value in data base.
I want to only how to update and delete button value in database. in same table and same index.
syns in this check box button.
I tried to many times but do not save and update properly. How it possible please help. Thank you
ViewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
selectedButton = [[NSMutableArray alloc]init];//i've already defined the array at the .h file
for (int i = 0; i<90000; i++) //yourTableSize = how many rows u got
{
[selectedButton addObject:@"NO"];
}
}
Table View
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//NSLog(@"tableview cell");
All_Receipt_Cell *cell = [_table_view dequeueReusableCellWithIdentifier:@"htrcell"];
if (cell==nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];
[cell.date_lbl setText:[device valueForKey:@"date"]];
[cell.company_lbl setText:[device valueForKey:@"company"]];
[cell.total_lbl setText:[device valueForKey:@"total"]];
cell.currency_lbl.text = @"$";
UIImage *img = [UIImage imageNamed:@"images.jpeg"];
UIButton*toggleButton= cell.toggleButton;
[toggleButton setImage:img forState:UIControlStateNormal];
img = [UIImage imageNamed:@"images.png"];
[toggleButton setImage:img forState:UIControlStateSelected];
[toggleButton setTag:indexPath.row+100];//set the tag whichever way you wanted it, i set it this way so that the button will have tags that is corresponding with the table's indexpath.row
[toggleButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:toggleButton];
//and now we set the button's selected state, everytime the table reuse/redraw the cell the button will set it's selected state according to the array
if([[selectedButton objectAtIndex:indexPath.row]isEqualToString:@"NO"])
{
[toggleButton setSelected:NO];
}
else
{
[toggleButton setSelected:YES];
}
return cell;
}
-(void)buttonPressed:(UIButton*)sender
{
int x = sender.tag - 100; //get the table's row
if([sender isSelected] ) //if the button is selected, deselect it, and then replace the "YES" in the array with "NO"
{
[selectedButton replaceObjectAtIndex:x withObject:@"NO"];
[sender setSelected:NO];
NSLog(@"unselected");
NSFetchRequest *fetchRequest=[NSFetchRequest fetchRequestWithEntityName:@"Receipt_Details"];
Receipt_Detail *newVehicle=[self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
[newVehicle setValue:@"ankur" forKey:@"isDone"];
[self.managedObjectContext save:nil];
}
else if (![sender isSelected]) //if the button is unselected, select it, and then replace the "NO" in the array with "YES"
{
[selectedButton replaceObjectAtIndex:x withObject:@"YES"];
[sender setSelected:YES];
NSLog(@"selected %@",sender.titleLabel.text);
NSLog(@"tag number is = %ld",(long)[sender tag]);
NSFetchRequest *fetchRequest=[NSFetchRequest fetchRequestWithEntityName:@"Receipt_Details"];
Receipt_Detail *data_check=[self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
[data_check setValue:sender.titleLabel.text forKey:@"isDone"];
[self.managedObjectContext save:nil];
}
}