2012-08-01 111 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)..