删除任何行总是删除最后一行而不是在ios目标c中删除多行

时间:2017-03-31 08:04:46

标签: ios objective-c uitableview delete-row

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

           return result.count;

        }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

       static NSString *CellIdentifier = @"cell";

       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


       if (cell) {


           name = (UILabel *)[cell viewWithTag:10];
           name.text = [result objectAtIndex : indexPath.row];
       }

       else{

           name.tag=indexPath.row;
       }

       return cell;


    }


    - (IBAction)butn:(id)sender {


       [myTbl setEditing:YES animated:YES];

    }
    - (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
    {

       if (self.tbl.editing)
       {
           return UITableViewCellEditingStyleDelete;
       }

       return UITableViewCellEditingStyleNone;
    }

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath    

       if (editingStyle == UITableViewCellEditingStyleDelete) {

           [result removeObjectAtIndex:indexPath.row];
           [self myMethod];//in this method i'm deleting the data from api also
           [tableView reloadData];

       }

    }



    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
       return YES;
    }



  -(void)myMethod{


        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.remove.php"]];
        NSData *data = [string dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
        [request setHTTPMethod:@"POST"];
        [request setHTTPBody:data];


        NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration
defaultSessionConfiguration]];
        [[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable
 response, NSError * _Nullable error) {
            NSDictionary *JSON= [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers
 error:nil];

            NSArray *alertArray = [JSON objectForKey:@"removebookmark"];



      for (NSDictionary *alert in alertArray )

          {

                if ([[alert objectForKey:@"status"] isEqualToString:@"remove"])


                {
                                nslog(@"removebookmark");
                                [spinner stopAnimating];

                }

            }



        }]resume];


     }

@end

1 个答案:

答案 0 :(得分:0)

我可以看到您从本地阵列中删除对象,而不是更新服务器有关已删除的记录。

现在您要求删除记录列表(通过myMethod功能),显然该记录不在您的网络列表中。

因此,在重新加载列表后,该记录将会显示,并且您将总记录减1,这样您就无法看到最后一条记录。

也不要重新加载完整的表格,只需根据需要重新加载行和部分。