Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 通过DidSelectRowatineXpath将NSManagedObject添加到NSMutableArray_Ios_Objective C_Uitableview_Core Data_Didselectrowatindexpath - Fatal编程技术网

Ios 通过DidSelectRowatineXpath将NSManagedObject添加到NSMutableArray

Ios 通过DidSelectRowatineXpath将NSManagedObject添加到NSMutableArray,ios,objective-c,uitableview,core-data,didselectrowatindexpath,Ios,Objective C,Uitableview,Core Data,Didselectrowatindexpath,我已经建立了一个核心数据项目,我有一个NSFetchedResultsController工作正常,NSPredicates工作正常,现在我开始对屏幕上显示的数据做一些有用的事情 现在,我的UITableViewCellAccessority工作正常,可以切换选中/未选中的单元格,但当我尝试通过didselectRowatinedExath将selectedManagedObject添加到myArrayOfManagedObjects时,返回的是“零” 通过didSelectRowAtIndex

我已经建立了一个核心数据项目,我有一个
NSFetchedResultsController
工作正常,
NSPredicates
工作正常,现在我开始对屏幕上显示的数据做一些有用的事情

现在,我的UITableViewCellAccessority工作正常,可以切换选中/未选中的单元格,但当我尝试通过
didselectRowatinedExath
selectedManagedObject
添加到
myArrayOfManagedObjects
时,返回的是“零”

通过
didSelectRowAtIndexPath
NSMutableArray
添加/删除
NSManagedObject
的正确方法是什么

以下是相关属性:

@property (nonatomic, strong) NSManagedObject *selectedManagedObject;
@property (nonatomic, strong) NSMutableArray *myArrayOfManagedObjects;
这是我的
viewDidLoad
,其中实例化了我要转储的`NSManagedObject'数组:

- (void)viewDidLoad {
    [super viewDidLoad];

    // Standard Core Data Setup Stuff

    // Instantiate a list to store myArrayOfManagedObjects
    self.myArrayOfManagedObjects = [[NSMutableArray alloc]init];
}
这是我的
didSelectRowAtIndexPath

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

        NSManagedObject *selectedManagedObject = self.selectedManagedObject;

        // Set the checkmark accessory for the selected row.
        // TODO: add the object to the array
        if ([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryNone) {
            [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
            // ** My problem lives here
            [self.myArrayOfManagedObjects insertObject:self.selectedManagedObject atIndex:indexPath.row];
            NSLog(@"Added %@ to myArrayOfManagedObjectsArray", selectedManagedObject.myObjectDescription);
            NSLog(@"myArrayOfManagedObjects contains %@", self.myArrayOfManagedObjects);
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
        } else {
            [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
            // ** My problem lives here
            [self.myArrayOfManagedObjects removeObjectAtIndex:indexPath.row];
            NSLog(@"Removed %@ from myArrayOfManagedObjectsArray", selectedManagedObject.myObjectDescription);
            NSLog(@"myArrayOfManagedObjects contains %@", self.myArrayOfManagedObjects);
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
        }
    }
更新:问题已解决

如果不清楚,我的
表视图
上的内容来自
fetchedResultsController
。我的任务是将其中的项目添加到一个单独的
NSMutableArray
,然后保存。以下是它的工作原理:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // ** Problem Solved: I needed to tell the compiler where my selectedManagedObject was
    self.selectedManagedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];

    // Set the checkmark accessory for the selected row.
    if ([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryNone) {
        [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
        [self.myArrayOfManagedObjects addObject:self.selectedManagedObject];
        NSLog(@"Added **%@** to myArrayOfManagedObjects", self.selectedManagedObject.description);
        NSLog(@"myArrayOfManagedObjects contains %lu objects", (unsigned long)[self.myArrayOfManagedObjects count]);
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    } else {
        [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
        [self.myArrayOfManagedObjects removeObject:self.selectedManagedObject];
        NSLog(@"Removed **%@** from myManagedObjectArray", self.selectedManagedObject.description);
        NSLog(@"myArrayOfManagedObjects contains %lu objects", (unsigned long)[self.myArrayOfManagedObjects count]);
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
}

我认为你的逻辑有问题。也许我不明白

您有一个包含当前显示的所有对象的数组,对吗?这是self.myArrayofManagedObjects。我想你是在别的地方填的,否则你就不会问这个问题了

然后选择并执行以下操作:

NSManagedObject *selectedManagedObject = self.selectedManagedObject;
这意味着(正如您明确知道的)您新实例化的
NSManagedObject
现在与您的.h中的相同,从我所看到的,它没有实例化

所以你几乎在玩
nil
这一行后面的每一行

您可能应该做的是:

NSManagedObject *selectedManagedObject = [self.myArrayOfManagedObjects objectAtIndex:indexPath.row];

现在,在对象数组中有了对正确对象的引用,可以开始操作它了

从我看到的情况来看,您希望将该对象添加或删除到某个数组中,因此您只需添加或删除对新数组的引用即可

[myArray addObject:OBJECT];
[myArray removeObject:OBJECT];
因为他们是推荐人,他们会找到的

如果要修改构建tableview的同一数组的数据,则必须调用

[self.tableView reloadData];

要查看最新更新的tableview,以便在可变数组中添加对象,请执行以下操作:

[self.myArrayOfManagedObjects addObject:obj];
[self.myArrayOfManagedObjects removeObject:obj];
用于从可变数组中删除对象:

[self.myArrayOfManagedObjects addObject:obj];
[self.myArrayOfManagedObjects removeObject:obj];

tableView
显示来自
fetchedResultsController
的内容。我正在尝试从
表视图中选择
NSManagedObject
s,并将它们添加到
NSMutableArray
以保存到磁盘。您的解决方案让我更进一步了,但我得到了以下错误:***由于未捕获的异常“NSRangeException”而终止应用程序,原因:“***-[\uu NSArrayM objectAtIndex:]:索引1超出了空数组的边界”`这似乎很清楚,您正试图在空数组中的索引1处进行交互。当你指的是保存到磁盘时,你指的是写一个文件还是简单地保存你的上下文/持久性存储?我计划保存
selectedManagedObjects的
NSMutableArray
,然后在另一个
viewController
上检索它们。好的,那么你只需要在另一个数组中保存它们,你需要做的非常简单:在didSelect上,检查对象是否已在数组中。如果是,请将其删除,如果不是,请添加。我建议您使用断点来查看它们是否正确地添加到数组中,以及它们是否应该删除。异常究竟是什么时候发生的?另外,你能用新代码编辑你的帖子,并在崩溃的那一行加上评论吗?当你用新信息做了这些事情时,请在这里通知我:)我是在你和Aneeq Anwar的帮助下弄明白的。我把
addObject
部分改为:`self.selectedManagedObject=[self.fetchedResultsController objectAtIndexPath:indexPath]`它可以完美地工作。谢谢你们,谢谢你们的帮助。在你的帮助下,我找到了选择正确“事物”所需要做的事情。