首页 文章

更新核心数据中的值

提问于
浏览
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,我应该能够编辑值并更新核心数据 . 任何人都告诉我如何实现这一目标?

1 回答

  • 1

    首先,我建议您使用NSFetchedResultsController从核心数据填充表视图(反之亦然) . 其次,当你从tableview中单击一个单独的行时,使用tableview的委托,就像我在下面所做的那样(对于这个在app委托中,我声明了一个名为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;
    
    }
    

    当你回到第一个视图conrtoller时,从核心数据中获取数据并获取所选索引路径的数据参数(再次通过 sharedData.selectedMesageIndex ) .

相关问题