1

UITextView テキストを表示するための UITableView セルがあります。を使用してセル行のテキストを削除する必要があります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

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 に答える