Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 未在UITableView中删除indexpath_Ios_Iphone_Uitableview_Indexing - Fatal编程技术网

Ios 未在UITableView中删除indexpath

Ios 未在UITableView中删除indexpath,ios,iphone,uitableview,indexing,Ios,Iphone,Uitableview,Indexing,我有UITableView单元格用于显示UITextView文本。我需要使用UITableViewCellEditingStyleDelete删除一些单元格行文本。当我使用编辑按钮删除一些文本时,我得到了一个错误 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSMutableArray* myMutableArrayAgain = [NSMutabl

我有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__);
}
-(无效)编辑按钮按下:(UIButton*)按钮{
NSLog(@“>>>输入%s>输入%s>输入%s>输入%s确定尝试此操作,
我使用的是正常的字符串值,最好检查数组是否正确地从用户默认值加载


  @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
@界面视图控制器()
{
NSMutableArray*mymutableArrayGain;//成员mut_数组这是数组
}
@结束
@实现视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
//例如,您正在设置数组中的值
NSUserDefaults*standardDefaults=[NSUserDefaults standardUserDefaults];
NSMUTABLEARRY*arrayObj=[[NSMUTABLEARRY alloc]init];

对于(int i=0;i您需要在删除后添加synchronize语句,如下所示更改您的函数

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

为什么要从数组中删除对象两次删除或注释[myMutableArrayAgain removeObjectAtIndex:indexPath.row];任何一个line@Shan:同样的,删除后它不工作..从此行NSArray*indexPathsToRemove=[NSArray arrayWithObject:indexPath];有问题它不工作。添加此行后有错误[tableView DeleteRowsAndExpaths:InExpathStore随RowAnimation移动:UITableViewRowAnimationRight];此行警告[tableView1 DeleteRowsAndExpaths:[NSArray Array WithObject:path]随RowAnimation:UITableViewScrollPositionBottom];尝试此“UITableViewRowAnimationFade”"让我们看看它正在删除。但在关闭应用程序后或返回到viewcontroller后,它会显示所有删除的值删除viewcontroller后,它会显示所有删除的值。为什么会发生这种情况?可能您正在NSUserDefaults和Synching中的某个位置再次填写所有详细信息,请检查您的代码。空值显示in表..所以它不是删除正确的索引如果您使用我提供的更新代码,则必须解决崩溃问题,但其余问题需要彻底调试并解决。如果有帮助,请标记问题,如果有其他问题,请发布新问题(如果可能)。
- (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__);
}
#ifdef DEBUG
#   define NLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#endif