简体   繁体   中英

indexpath not removing in UITableView

I have UITableView cell for display UITextView text. I need to delete some cell rows text using UITableViewCellEditingStyleDelete . When i use edit button for delete some text i got error.

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


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

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

    return [myMutableArrayAgain count];

}

Delete function:

-(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__);
}

Error in 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).'

在此输入图像描述

You need to add synchronize statement after deleting, change your function as shown below.

- (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__);
}

Also you can use following code to redefine NSLog and have just print variables or strings which will give you method names and all, define these anywhere in 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

ok try this, i am used the normal string values, u better check if the array is correctly loading from the user defaults


  @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


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM