Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/122.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.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 当我有多个分区时,尝试从UITableView中删除行时发生崩溃。节值位于动态NSMutableDictionary中_Ios_Objective C_Uitableview_Nsdictionary_Delete Row - Fatal编程技术网

Ios 当我有多个分区时,尝试从UITableView中删除行时发生崩溃。节值位于动态NSMutableDictionary中

Ios 当我有多个分区时,尝试从UITableView中删除行时发生崩溃。节值位于动态NSMutableDictionary中,ios,objective-c,uitableview,nsdictionary,delete-row,Ios,Objective C,Uitableview,Nsdictionary,Delete Row,让我描述一下我到底想要什么。我有一本字典,其中键是字母表,它的值是从字母表开始的 大概是这样的: key value --------------------------------------------------------- A (Apple, Aim, Arise, Attempt, Airplane, Absolute) B (Bubble, Bite, Borrow, Basket) C (Cat, Correct) D (D

让我描述一下我到底想要什么。我有一本字典,其中键是字母表,它的值是从字母表开始的

大概是这样的:

key     value
---------------------------------------------------------
A       (Apple, Aim, Arise, Attempt, Airplane, Absolute)
B       (Bubble, Bite, Borrow, Basket)
C       (Cat, Correct)
D       (Dog, Direction, Distribute)
我的
NSMutableDictionary
call
wordDic
按字母顺序排列所有数据。就像上表一样。我从我的
ManagedObject
中提取所有数据,并在该字典中显示它们

- (void)dataStructure
{
    NSManagedObjectContext *moc = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Words"];

    self.wordListArray = [[NSMutableArray alloc] init];
    self.wordListArray = [[moc executeFetchRequest:fetchRequest error:nil] mutableCopy];

    self.wordInWordListArray = [[NSMutableArray alloc] init];

    for (int i = 0; i < [self.wordListArray count]; i++)
    {
        self.words = [self.wordListArray objectAtIndex:i];
        [self.wordInWordListArray addObject:self.words.word];
    }

    if ([self.wordListArray count] != 0)
    {
        self.wordDic = [self sortedDictionary:self.wordInWordListArray];

        // Sorted key array
        self.keyArray = [[NSArray alloc] init];
        NSArray *key = [[NSArray alloc] init];
        key = [self.wordDic allKeys];
        self.keyArray = [key sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    }

    [self.homeTableView reloadData];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self viewRelatedChange];
    [self dataStructure];
}
但它不起作用。我的应用程序仍然会因为同样的消息而崩溃。我还注意到,在
numberofrowsinssection
中,
section
的值并不总是以0开头。(如
indexPath.row
did)。那么,我的结构有什么问题?解决办法是什么

下面是我的
tableView
的完整代码。如果有人有答案,请与我分享。这将是非常可观的。事先非常感谢。大家好

- (void)dataStructure
{
    NSManagedObjectContext *moc = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Words"];

    self.wordListArray = [[NSMutableArray alloc] init];
    self.wordListArray = [[moc executeFetchRequest:fetchRequest error:nil] mutableCopy];

    self.wordInWordListArray = [[NSMutableArray alloc] init];

    for (int i = 0; i < [self.wordListArray count]; i++)
    {
        self.words = [self.wordListArray objectAtIndex:i];
        [self.wordInWordListArray addObject:self.words.word];
    }

    if ([self.wordListArray count] != 0)
    {
        self.wordDic = [self sortedDictionary:self.wordInWordListArray];

        // Sorted key array
        self.keyArray = [[NSArray alloc] init];
        NSArray *key = [[NSArray alloc] init];
        key = [self.wordDic allKeys];
        self.keyArray = [key sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    }

    [self.homeTableView reloadData];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self viewRelatedChange];
    [self dataStructure];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.keyArray count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 28;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"section %ld", (long)section);
    NSString *key = [self.keyArray objectAtIndex:section];
    NSMutableArray *value = [self.wordDic objectForKey:key];

   // NSLog(@"keys %@", key);
    NSLog(@"values %@", value);

    return [value count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([selectedIndex isEqual:indexPath]) {
        return kHomeTableViewExpandedCellHeight;
    } else {
        return kHomeTableViewCellHeight;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString *cellIdentifier = kHomeTableViewCellID;
        HomeTableViewCell *cell = (HomeTableViewCell *)[self.homeTableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (!cell)
        {
            cell = [[HomeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        NSString *secTitle = [self.keyArray objectAtIndex:indexPath.section];
        secData = [self.wordDic objectForKey:secTitle];
        [secData sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
        NSString *data = [secData objectAtIndex:indexPath.row];
        [cell.wordLabel setText:data];

//        NSManagedObject *words = [self.wordListArray objectAtIndex:indexPath.row];
//        [cell.wordLabel setText:[NSString stringWithFormat:@"%@", [words valueForKey:@"word"]]];

        return cell;
}

- (void)tableView:(UITableView *) tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NSString *secTitle = [self.keyArray objectAtIndex:indexPath.section];
    NSString *selectedString = [secData objectAtIndex:indexPath.row];

    NSString *key = [self.keyArray objectAtIndex:indexPath.section];
    NSMutableArray *words = [self.wordDic objectForKey:key];
    [words sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

    NSManagedObjectContext *context = [self managedObjectContext];
    NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Words" inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDesc];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"word like %@", [secData objectAtIndex:indexPath.row]];
    [request setPredicate:predicate];

    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        NSError *error;
        NSArray *matchingData = [context executeFetchRequest:request error:&error];

        for (NSManagedObject *obj in matchingData)
        {
            [context deleteObject:obj];
        }
        [context save:&error];

        // remove info from tableView array
        int coreDataIndex = 0;
        for (int i = 0; i < [self.wordInWordListArray count]; i++)
        {
            NSString *dataString = [self.wordInWordListArray objectAtIndex:i];

            if ([dataString isEqualToString:selectedString])
            {
                coreDataIndex = i;
            }
        }

        [self.wordListArray removeObjectAtIndex:coreDataIndex];
        [words removeObjectAtIndex:indexPath.row];
        //[self.homeTableView beginUpdates];
        [self.homeTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        //[self.homeTableView endUpdates];
    }
    [self dataStructure];
}
-(void)数据结构
{
NSManagedObjectContext*moc=[self-managedObjectContext];
NSFetchRequest*fetchRequest=[[NSFetchRequest alloc]initWithEntityName:@“Words”];
self.wordlistary=[[NSMutableArray alloc]init];
self.wordListArray=[[moc executeFetchRequest:fetchRequest错误:nil]mutableCopy];
self.wordInWordListArray=[[NSMutableArray alloc]init];
对于(int i=0;i<[self.wordlistary count];i++)
{
self.words=[self.wordlistary objectAtIndex:i];
[self.wordInWordListArray addObject:self.words.word];
}
如果([self.wordlistary count]!=0)
{
self.wordDic=[self-sortedDictionary:self.wordinwordlistary];
//排序键数组
self.keyArray=[[NSArray alloc]init];
NSArray*键=[[NSArray alloc]init];
key=[self.wordDic allkey];
self.keyArray=[key sortedarrayingselector:@selector(localizedCaseInsensitiveCompare:)];
}
[self.homeTableView重载数据];
}
-(无效)视图将显示:(BOOL)动画
{
[超级视图将显示:动画];
[自我观点相关变化];
[自身数据结构];
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
返回[self.keyArray count];
}
-(CGFloat)表格视图:(UITableView*)表格视图头部高度部分:(NSInteger)部分
{
返回28;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
NSLog(@“段%ld”,(长)段);
NSString*key=[self.keyArray objectAtIndex:section];
NSMutableArray*值=[self.wordDic objectForKey:key];
//NSLog(@“键%@”,键);
NSLog(@“值%@”,值);
返回[值计数];
}
-(CGFloat)tableView:(UITableView*)表视图行高度索引路径:(NSIndexPath*)索引路径
{
if([selectedIndex isEqual:indexPath]){
返回KHOMetablieWebExpandedCellHeight;
}否则{
返回参数“设置单元格高度”;
}
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*cellIdentifier=KhomateLeviewCellid;
HomeTableViewCell*单元格=(HomeTableViewCell*)[self.homeTableView出列重用CellWithIdentifier:cellIdentifier];
如果(!单元格)
{
cell=[[HomeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
NSString*secTitle=[self.keyArray objectAtIndex:indexath.section];
secData=[self.wordDic objectForKey:secTitle];
[secData sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSString*data=[secData objectAtIndex:indexath.row];
[cell.wordLabel setText:data];
//NSManagedObject*words=[self.wordListArray objectAtIndex:indexPath.row];
//[cell.wordLabel setText:[NSString stringWithFormat:@“%@,[words valueForKey:@“word”]];
返回单元;
}
-(void)tableView:(UITableView*)tableView提交的编辑样式:(UITableViewCellEditingStyle)行的编辑样式索引路径:(NSIndexPath*)索引路径
{
//NSString*secTitle=[self.keyArray objectAtIndex:indexath.section];
NSString*selectedString=[secData objectAtIndex:indexath.row];
NSString*key=[self.keyArray objectAtIndex:indexPath.section];
NSMutableArray*words=[self.wordDic objectForKey:key];
[单词sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSManagedObjectContext*上下文=[self-managedObjectContext];
NSEntityDescription*EntityDescription=[NSEntityDescription entityForName:@“Words”在托管对象上下文:上下文中];
NSFetchRequest*request=[[NSFetchRequest alloc]init];
[请求集合实体:entityDesc];
NSPredicate*谓词=[NSPredicate predicateWithFormat:@“word-like%@,[secData-objectAtIndex:indexath.row]];
[请求集谓词:谓词];
如果(editingStyle==UITableViewCellEditingStyleDelete)
{
n错误*错误;
NSArray*matchingData=[context executeFetchRequest:请求错误:&error];
用于(匹配数据中的NSManagedObject*obj)
{
[上下文删除对象:obj];
}
[上下文保存:&错误];
//从tableView数组中删除信息
int coreDataIndex=0;
for(int i=0;i<[self.wordinwordlistary count];i++)
{
NSString*dataString=[self.wordInWordListArray对象索引:i];
if([dataString IsequalString:selectedString])
{
coreDataIndex=i;
}
}
[self.wordListArray removeObjectAtIndex:coreDataIndex];
[words removeObjectAtIndex:indexath.row];
//[self.homeTableView开始更新];
[self.homeTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]带RowAnimation:UITableViewRowAnimationFade];
//[self.homeTableView endUpdates];
}
[自我数据结构]
- (void)dataStructure
{
    NSManagedObjectContext *moc = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Words"];

    self.wordListArray = [[NSMutableArray alloc] init];
    self.wordListArray = [[moc executeFetchRequest:fetchRequest error:nil] mutableCopy];

    self.wordInWordListArray = [[NSMutableArray alloc] init];

    for (int i = 0; i < [self.wordListArray count]; i++)
    {
        self.words = [self.wordListArray objectAtIndex:i];
        [self.wordInWordListArray addObject:self.words.word];
    }

    if ([self.wordListArray count] != 0)
    {
        self.wordDic = [self sortedDictionary:self.wordInWordListArray];

        // Sorted key array
        self.keyArray = [[NSArray alloc] init];
        NSArray *key = [[NSArray alloc] init];
        key = [self.wordDic allKeys];
        self.keyArray = [key sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    }

    [self.homeTableView reloadData];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self viewRelatedChange];
    [self dataStructure];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.keyArray count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 28;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"section %ld", (long)section);
    NSString *key = [self.keyArray objectAtIndex:section];
    NSMutableArray *value = [self.wordDic objectForKey:key];

   // NSLog(@"keys %@", key);
    NSLog(@"values %@", value);

    return [value count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([selectedIndex isEqual:indexPath]) {
        return kHomeTableViewExpandedCellHeight;
    } else {
        return kHomeTableViewCellHeight;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString *cellIdentifier = kHomeTableViewCellID;
        HomeTableViewCell *cell = (HomeTableViewCell *)[self.homeTableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (!cell)
        {
            cell = [[HomeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        NSString *secTitle = [self.keyArray objectAtIndex:indexPath.section];
        secData = [self.wordDic objectForKey:secTitle];
        [secData sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
        NSString *data = [secData objectAtIndex:indexPath.row];
        [cell.wordLabel setText:data];

//        NSManagedObject *words = [self.wordListArray objectAtIndex:indexPath.row];
//        [cell.wordLabel setText:[NSString stringWithFormat:@"%@", [words valueForKey:@"word"]]];

        return cell;
}

- (void)tableView:(UITableView *) tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NSString *secTitle = [self.keyArray objectAtIndex:indexPath.section];
    NSString *selectedString = [secData objectAtIndex:indexPath.row];

    NSString *key = [self.keyArray objectAtIndex:indexPath.section];
    NSMutableArray *words = [self.wordDic objectForKey:key];
    [words sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

    NSManagedObjectContext *context = [self managedObjectContext];
    NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Words" inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDesc];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"word like %@", [secData objectAtIndex:indexPath.row]];
    [request setPredicate:predicate];

    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        NSError *error;
        NSArray *matchingData = [context executeFetchRequest:request error:&error];

        for (NSManagedObject *obj in matchingData)
        {
            [context deleteObject:obj];
        }
        [context save:&error];

        // remove info from tableView array
        int coreDataIndex = 0;
        for (int i = 0; i < [self.wordInWordListArray count]; i++)
        {
            NSString *dataString = [self.wordInWordListArray objectAtIndex:i];

            if ([dataString isEqualToString:selectedString])
            {
                coreDataIndex = i;
            }
        }

        [self.wordListArray removeObjectAtIndex:coreDataIndex];
        [words removeObjectAtIndex:indexPath.row];
        //[self.homeTableView beginUpdates];
        [self.homeTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        //[self.homeTableView endUpdates];
    }
    [self dataStructure];
}
- (void)tableView:(UITableView *) tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *selectedString = [secData objectAtIndex:indexPath.row];

    NSString *key = [self.keyArray objectAtIndex:indexPath.section];
    NSMutableArray *words = [self.wordDic objectForKey:key];
    [words sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

    NSManagedObjectContext *context = [self managedObjectContext];
    NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Words" inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDesc];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"word like %@", [secData objectAtIndex:indexPath.row]];
    [request setPredicate:predicate];

    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        NSError *error;
        NSArray *matchingData = [context executeFetchRequest:request error:&error];

        for (NSManagedObject *obj in matchingData)
        {
            [context deleteObject:obj];
        }
        [context save:&error];

        // remove info from tableView array
        int coreDataIndex = 0;
        for (int i = 0; i < [self.wordInWordListArray count]; i++)
        {
            NSString *dataString = [self.wordInWordListArray objectAtIndex:i];

            if ([dataString isEqualToString:selectedString])
            {
                coreDataIndex = i;
            }
        }

        [self.wordListArray removeObjectAtIndex:coreDataIndex];
        [words removeObjectAtIndex:indexPath.row];
        [self dataStructure];

        //[self.homeTableView beginUpdates];
        [self.homeTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        //[self.homeTableView endUpdates];
    }
[words objectAtIndex:indexPath.row];