Chandra Sekhar
Chandra Sekhar

Reputation: 177

Updating the values in core data

I have a list of expenses and their price stored in core data in view1. I am retrieving these values in view2 and showing them in a tableView.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Displayin the values in a Cell.

NSManagedObject *records = nil;
        records = [self.listOfExpenses objectAtIndex:indexPath.row];
        self.firstLabel.text = [records valueForKey:@"category"];
        NSString *dateString = [NSString stringWithFormat:@"%@",[records valueForKey:@"date"]];
        NSString *dateWithInitialFormat = dateString;
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
        NSDate *date = [dateFormatter dateFromString:dateWithInitialFormat];
        [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
        NSString *dateWithNewFormat = [dateFormatter stringFromDate:date];
        self.secondLabel.text = dateWithNewFormat;
        [dateFormatter release];
        NSString *amountString = [NSString stringWithFormat:@"%@",[records valueForKey:@"amount"]];
        self.thirdLabel.text = amountString;
}

if i click on a row in the tableView it should navigate to view1 and i should be able to edit the values and update in the core data.Can any one tell me how to achieve this?

Upvotes: 0

Views: 220

Answers (1)

ilhnctn
ilhnctn

Reputation: 2210

First, i recommend you to use NSFetchedResultsController for populating table view from core data(vice versa). Second, when you clicked on an individual row from tableview, use the delegate of tableview as like i do below(for this in app delegate i declared an int variable called selectedMesageIndex)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   //YourAppDelegate is the name of your app delegate class
   YourAppDelegate* sharedDa= (YourAppDelegate *)([[UIApplication sharedApplication]delegate]);

   sharedData.selectedMesageIndex = indexPath.row;

}  

And when you come back to the first view conrtoller, fetch data from core data and get the data parameter of selected index path(again via sharedData.selectedMesageIndex )..

Upvotes: 1

Related Questions