1

我有 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).'

在此处输入图像描述

4

2 回答 2

3

您需要在删除后添加同步语句,更改您的功能如下所示。

- (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

检查此以获取有关 NSLog 格式的更多信息

于 2013-10-07T10:59:45.903 回答
0

好的,试试这个,我用的是普通的字符串值,你最好检查一下数组是否从用户默认值正确加载


  @interface ViewController ()<UITableViewDataSource , UITableViewDelegate>
   {
       NSMutableArray *myMutableArrayAgain; //member mut_array this is the array
   }

   @end

   @implementation ViewController

   - (void)viewDidLoad
     {

        [super viewDidLoad];

         //for example somewhere u are setting the values in the array 

         NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *arrayObj = [[NSMutableArray alloc] init];


       for(int i = 0 ; i<10 ; i++) {
       [arrayObj addObject:[NSString stringWithFormat:@"hello %d",i]];
       }

      [standardDefaults setObject:arrayObj forKey:@"save"]; //for key save
      [arrayObj release];


         //fetch the data once becz chancge's that u made to array to be reflected in tableview
          //i am fetching the contents hear
          myMutableArrayAgain = [[NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"save"]] retain]; //hear u are getting the contents of array

       //   myMutableArrayAgain = [[NSMutableArray arrayWithObjects:@"hello",@"world",@"happy",@"coding", nil]retain];//insted of ur values i use some string values , insted u can use alloc init method if confused of retain

   }


  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  {
       return 1;
  }

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

     // NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithArray:  [[NSUserDefaults standardUserDefaults] objectForKey:@"save"]]; //dont fech the values to be displayed it is alreday in "myMutableArrayAgain" array
     // NSLog(@"array is %@",myMutableArrayAgain);

    return [myMutableArrayAgain count];

   }

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
   {
     //   NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"save"]]; //dont fetch

     static NSString *CellIdentifier = @"Cell";

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

     if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

          }

    cell.textLabel.text = [myMutableArrayAgain objectAtIndex:indexPath.row]; //use the member array
    [cell.textLabel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:14]];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
  }


    - (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];
           [tableView1 beginUpdates];

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

         // NSArray *indexPathsToRemove = [NSArray arrayWithObject:indexPath];

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

         NSIndexPath *path=[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];

         [tableView1 deleteRowsAtIndexPaths:[NSArray arrayWithObject:path] withRowAnimation:UITableViewScrollPositionBottom];

       //  [tableView1 deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationRight];

        [tableView1 endUpdates];

     }

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


  - (void)dealloc 
  {
     [_aTableView release];
     [_aButton release];
      [super dealloc];
   }
  - (IBAction)whenButtonTapped:(id)sender
   {
       NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);

      [self.aTableView setEditing:![self.aTableView isEditing] animated:YES];

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

      [self.aButton setTitle:buttonTitle forState:UIControlStateNormal];

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


于 2013-10-07T09:24:42.890 回答