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,我应该能够编辑核心数据中的值和更新。谁能告诉我如何实现这一点?

4

1 回答 1

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)..

于 2012-08-01T13:37:20.450 回答