ios - 无法在UITableView中删除indexpath

标签 ios iphone uitableview indexing

我有UITableView单元格用于显示UITextView文本。我需要使用UITableViewCellEditingStyleDelete删除一些单元格行文本。当我使用编辑按钮删除一些文本时,出现错误。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


   NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"save"]];

    // NSLog(@"array is %@",myMutableArrayAgain);

    return [myMutableArrayAgain count];

}

删除功能:
-(void)editButtonPressed:(UIButton *)button{

    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);

    [tableView setEditing:![tableView isEditing] animated:YES];

    NSString *buttonTitle = ([tableView isEditing]) ? @"Done" : @"Edit";

    [editButton setTitle:buttonTitle forState:UIControlStateNormal];

    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}


- (void)tableView:(UITableView *)tableView1
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);


    NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"save"]];


    if (editingStyle == UITableViewCellEditingStyleDelete)
    {

        //first delete this from the db of favorites table
        NSLog(@"art id is %@",[myMutableArrayAgain objectAtIndex:indexPath.row]);





        NSLog(@"indexpath is %d", indexPath.row);

        [myMutableArrayAgain removeObjectAtIndex:indexPath.row];




        NSLog(@"remove %d",indexPath.row);

            NSArray *indexPathsToRemove = [NSArray arrayWithObject:indexPath];

        NSLog(@"indextoremove %@",indexPathsToRemove);

        [tableView1 deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationRight];

    }

    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}



- (void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
      toIndexPath:(NSIndexPath *)toIndexPath
{
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);

    NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"save"]];


    NSString *contentsToMove = [[myMutableArrayAgain objectAtIndex:[fromIndexPath row]] retain];

    [myMutableArrayAgain removeObjectAtIndex:[fromIndexPath row]];

    [myMutableArrayAgain insertObject:contentsToMove atIndex:[toIndexPath row]];

    [contentsToMove release];

    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}

Nslog中的错误:
2013-10-07 14:29:41.939 WorkSa[3689:c07] indexpath is 0
2013-10-07 14:29:41.940 WorkSa[3689:c07] remove 0
2013-10-07 14:29:41.940 WorkSa[3689:c07] indextoremove (
    "<NSIndexPath 0x8b44fa0> 2 indexes [0, 0]"
)
2013-10-07 14:29:41.941 WorkSa[3689:c07] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1046
2013-10-07 14:29:41.942 WorkSafeACT[3689:c07] *** 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 (7) must be equal to the number of rows contained in that section before the update (7), 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).'

最佳答案

删除后需要添加syncyize语句,如下所示更改功能。

- (void)tableView:(UITableView *)tableView1
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
    NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"save"]];
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        //first delete this from the db of favorites table
        NSLog(@"art id is %@",[myMutableArrayAgain objectAtIndex:indexPath.row]);
        NSLog(@"indexpath is %d", indexPath.row);
        [myMutableArrayAgain removeObjectAtIndex:indexPath.row];

        //Add these 2 lines - Similarly you will need to do in moving items.
        [[NSUserDefaults standardUserDefaults] setObject:myMutableArrayAgain forKey:@"save"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        //Add these 2 lines

        [myMutableArrayAgain removeObjectAtIndex:indexPath.row];
        NSLog(@"remove %d",indexPath.row);
        NSArray *indexPathsToRemove = [NSArray arrayWithObject:indexPath];
        NSLog(@"indextoremove %@",indexPathsToRemove);
        [tableView1 deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationRight];

    }

    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}

您也可以使用以下代码重新定义NSLog,并仅打印变量或字符串,这些变量或字符串将为您提供方法名称及所有名称,并在appdelegate中的任何位置定义
#ifdef DEBUG
#   define NLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#endif

Check this for more information on NSLog formatting

关于ios - 无法在UITableView中删除indexpath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19220781/

相关文章:

ios - Push Segue 触发一个 Modal Segue,导致没有导航栏的 VC

iphone - 如何测试一个点是否在 View 中

uitableview - 由deleteRowsAtIndexPaths引起的崩溃

ios - 节和行未正确显示在UITableView Swift中

ios - 通过蓝牙发送联系人

ios - 阻止 ScrollView 超出和低于边界

ios - iOS 中水印图像大小根据视频大小而变化

android - 电子商务应用程序的前端技术堆栈

iphone - iOS自动引用计数(ARC)向后兼容性?

ios - swift ,将单选按钮组添加到 TableView 单元格