2012-08-01 51 views
0

我有一个存储在核心数据view1中的开支及其价格列表。 我在view2中检索这些值,并在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; 
} 

,如果我对TableView中连续单击它应该导航到VIEW1,我应该能够编辑在核心data.Can任何一个可以告诉我如何做到这一点的价值和更新?

+0

您可能会使用'didSelectRowAtIndexPath' – Dustin 2012-08-01 13:32:19

+0

我知道如何导航,但我不知道如何更新值。 – 2012-08-01 13:33:19

+0

'[nameOfManagedObjectContext save:&error]' – Dustin 2012-08-01 13:34:57

回答

1

首先,我建议您使用NSFetchedResultsController从核心数据填充表视图(反之亦然)。其次,当你点击从tableview中的各行,使用的tableview的委托作为像我做以下(这在应用程序委托我宣布所谓selectedMesageIndex int变量)

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

} 

而当你回来的第一视图控制器,从核心数据获取数据,并获取所选索引路径的数据参数(再次通过sharedData.selectedMesageIndex)..