-1

我想删除我的 2 个 tableViews 之一中的一个单元格(1 个是主要的,2 个是收藏夹)。

要删除一个单元格,我应该将特定值设置为“NO”,以便在 plist 中为 NO(收藏夹表仅显示“isFav”值设置为 YES 的单元格)。

检查此问题以获取更多详细信息:UITableView not shows the right cells

回到我的问题,我试着做

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSArray *plist = [self readPlist];
    NSMutableDictionary *theItem = [[[plist objectAtIndex:indexPath.section] valueForKey:@"Rows"] objectAtIndex:indexPath.row]; 
    NSLog(@"%@",theItem);
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [theItem setValue:[NSNumber numberWithBool:NO] forKey:@"isFav"]; 

        [self.tableView deleteRowsAtIndexPaths:[[[NSArray arrayWithObject:[plist objectAtIndex:indexPath.section]]valueForKey:@"Rows"]objectAtIndex:indexPath.row] withRowAnimation:UITableViewRowAnimationNone];

        [self writePlist:plist];   
        [self.tableView reloadData];

    }
}


- (void)writePlist:(NSArray*)arr 
{ 
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
    NSString *plistPath = [[documentPaths lastObject] stringByAppendingPathComponent:@"tipsList.plist"]; 
    NSFileManager *fMgr = [NSFileManager defaultManager]; 
    if ([fMgr fileExistsAtPath:plistPath]) 
        [fMgr removeItemAtPath:plistPath error:nil]; 

    [arr writeToFile:plistPath atomically:YES]; 
}

- (NSArray*)readPlist 
{  
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
    NSString *plistPath = [[documentPaths lastObject] stringByAppendingPathComponent:@"tipsList.plist"]; 
    NSFileManager *fMgr = [NSFileManager defaultManager]; 
    if (![fMgr fileExistsAtPath:plistPath]) { 
        plistPath = [[NSBundle mainBundle] pathForResource:@"tipsList" ofType:@"plist"]; 

    } 

    NSMutableArray *returnArr = [NSMutableArray arrayWithContentsOfFile:plistPath]; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isFav == YES"]; 

    for (NSDictionary *sect in returnArr) { 
        NSArray *arr = [sect objectForKey:@"Rows"]; 
        [sect setValue:[arr filteredArrayUsingPredicate:predicate] forKey:@"Rows"]; 
        [self.tableView reloadData];

    } 
    return returnArr;

}

但没有成功。我想做什么:尝试获取表中的当前项目,然后将其“isFav”值设置为 NO,然后从表中删除单元格,但我失败了,得到

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary row]: unrecognized selector sent to instance 0x7fd4ab0'

我试着做,[NSArray arrayWithObject:indexPath]但我明白了

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

任何帮助表示赞赏:|

4

2 回答 2

1

发生此错误是因为dataSource计数与实际数据不同。当您删除一个项目时,tableView单元格将下降,例如。从 10 到 9。但是你没有从你的数据中删除数据,dataSource所以你最终得到了不同的数据count,Xcode 戳了你一点。

正如在聊天中(广泛地)讨论的那样,我们用这段代码解决了这个问题:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [tableView beginUpdates];

        NSIndexPath *realIndex = [self realIndexPathForIndex:indexPath];
        NSArray *plist = [self readFullPlist];

        NSMutableDictionary *theItem = [[[plist objectAtIndex:realIndex.section] valueForKey:@"Rows"] objectAtIndex:realIndex.row];
        [theItem setValue:[NSNumber numberWithBool:NO] forKey:@"isFav"];
        [self writePlist:plist];

        [self refreshTable];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        [tableView endUpdates];
    }
}

- (NSArray*)readFullPlist
{
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
    NSString *plistPath = [[documentPaths lastObject] stringByAppendingPathComponent:@"tipsList.plist"]; 
    NSFileManager *fMgr = [NSFileManager defaultManager]; 
    if (![fMgr fileExistsAtPath:plistPath]) { 
        NSString *bundlePlistPath = [[NSBundle mainBundle] pathForResource:@"tipsList" ofType:@"plist"];
        [self writePlist:[NSArray arrayWithContentsOfFile:bundlePlistPath]];
    }

    return [NSArray arrayWithContentsOfFile:plistPath];
}

- (NSIndexPath*)realIndexPathForIndex:(NSIndexPath*)idxPath
{
    NSArray *fullList = [self readFullPlist];
    NSArray *subArr = [[fullList objectAtIndex:idxPath.section] objectForKey:@"Rows"];

    int row = idxPath.row;
    int newRow = 0;

    for (NSDictionary *dic in subArr)
    {
        if ([[dic valueForKey:@"isFav"] boolValue]) {
            if (row == 0) {
                return [NSIndexPath indexPathForRow:newRow inSection:idxPath.section];
            }
            row--;
        }
        newRow++;
    }
    return idxPath;
}
于 2012-06-29T00:10:32.057 回答
0

在您的 tableView: commitEditingStyle: 方法中,查找要删除 tableView 中行的语句。在这里你应该交出一个带有 NSIndexPaths 的数组。这一行很难在您的代码段中阅读,因为它包含许多嵌套调用......

于 2012-06-27T15:52:13.007 回答