繁体   English   中英

更新核心数据中的值

[英]Updating the values in core data

我在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,并且我应该能够编辑值并更新核心数据。有人可以告诉我如何实现这一点吗?

首先,我建议您使用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 )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM