2

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

enter image description here

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];


            }
     }

1 Answers1

1

The problem lies in your buttonPressed method. You fetch ALL the Receipt_Details objects. Although you declare newVehicle and data_check as Receipt_Detail objects, executeFetchRequest always returns an array of objects (even if there is only one object), so they are both NSArrays.

When you call setValue:forKey: on an NSArray, it calls setValue:forKey: on every element of the array. So ALL the Receipt_Details objects have their isDone property set to "ankur".

You presumably want only the Receipt_Details object for the relevant row to be updated. You could achieve this by adding a predicate to the fetch request, to return only the relevant object. But it's unwise to be executing a fetch request in the middle of a button action - the fetch could take some time and the user might observe a delay. Better and easier to use the pre-existing array of fetched objects (I assume that's what self.devices comprises), since you can use the row as a direct index on that array to locate the correct object, eg.

Receipt_Detail *newVehicle=[self.devices objectAtIndex:x];

You can also probably do away with the selectedButtons array, since you can use the isDone property on each object to indicate whether the relevant row should be checked.

pbasdf
  • 21,386
  • 4
  • 43
  • 75
  • Have you seen any method `setValue:forKey:` in `NSArray`. I haven't till now, still wondering how @ankur it is working. As `execiteFetchRequest` returns an NSArray not object. – Sachin Vas Sep 24 '16 at 16:58
  • @New16 I knew I had seen this documented - finally found it - see the Key Value Coding section in the [NSArray reference](https://developer.apple.com/reference/foundation/nsarray). – pbasdf Sep 24 '16 at 18:19
  • @pbasdf finally you resolved it. thank you so much. its working very good and update on related section. thank you :) – ankur kumawat Sep 26 '16 at 05:02