删除任何行时,总是删除最后一行,并且无法删除iOS目标C中的多行


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

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

现在您正在请求已删除记录的列表(通过myMethod函数(,显然该记录将不在您的Web列表中。

因此,该记录将在重新加载列表后可见,并且由于您将总记录减少 1,因此您将无法看到最后一条记录。

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

最新更新