从核心数据分配的变量变为空

时间:2013-11-20 10:29:15

标签: ios uitableview core-data segue

在我的应用中,我正在实施核心数据。执行fetch后,我使用获取的数据作为表视图的数据源。

我不太熟悉将NSFetchedResultsController集成为表数据源,所以我的方式不同。

dataSourceItems = [[NSMutableArray alloc]initWithArray:[[myCoreData fetchedResultsController] fetchedObjects]];

NSLogging“dataSourceItems”返​​回获取的结果(所以直到现在它正常工作)。我将它用作tableView数据源,我的单元格中填充了它的数据。

点击单元格后,我正在推动另一个(项目详细信息)视图控制器。我需要传递项目ID,以便我可以从核心数据中进行另一次获取。

     -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
         MainItemListCell *cell = [_tblViewitems cellForRowAtIndexPath:indexPath];
        NSLog(@"%@",cell.itemId);

        ItemDetailsViewController *itemDetail = [[ItemDetailsViewController alloc]
                                                                     initWithNibName:@"ItemDetailsViewController"
                                                                     bundle:nil];
        itemDetail.passedName = [NSString stringWithFormat:@"%@",cell.lblItemName.text];
        itemDetail.passedValue = cell.lblAmount.text;
        itemDetail.passedEvent =[dataSourceItems objectAtIndex:indexPath.row];
        NSLog(@"Assigning %@",((MyCoreDataItem *)[dataSourceItems objectAtIndex:indexPath.row]).cEventId);
        itemDetail.passedId = ((MyCoreDataItem *)[dataSourceItems objectAtIndex:indexPath.row]).cEventId;
        itemDetail.passedStatus = cell.lblOweStatus.text;


    [self.navigationController pushViewController:itemDetail animated:YES];
}

来自我的cellForRowAtIndexPath

Printing description of cell->_itemId:
102

itemDetail.passedId = cell._itemId;

NSLogging itemDetail.passedId之后我得到null

我也尝试过:

itemDetail.passedId = ((ItemDetail *)[dataSourceItems objectAtIndex:indexPath.row]).cEventId;

NSLog(@"Assigning %@",((ItemDetail *)[dataSourceItems objectAtIndex:indexPath.row]).cEventId);

Assigning (null)

NSLogging dataSourceItems返回以下内容:

Printing description of dataSourceItems:
<__NSArrayM 0x176815e0>(
<ItemDetail: 0x176828a0> (entity: ItemDetail; id: 0x17681870 <x-coredata://7E065CF5-7AC9-4BBB-A3F9-05DC5B155F60/ItemDetail/p1> ; data: <fault>)
)

起初我认为数据:错误意味着错误。但是,我found out表示错误不是错误。

然而,我真的迷失了为什么我无法将核心数据值传递给我的详细VC。

1 个答案:

答案 0 :(得分:0)

首先必须创建核心数据实体,然后将其相关属性分配给相关的 ItemDetailsViewController 的对象属性,如下所示。

MyCoreDataItem *myCoreDataItem = (MyCoreDataItem *)[dataSourceItems objectAtIndex:indexPath.row];
itemDetail.passedId = myCoreDataItem.itemId;

注意: - 您必须创建一个继承 NSManagedObject 的类,该类名为 MyCoreDataItem ,表示核心数据实体。您必须声明 itemId 等属性以及您想要的其他内容。