2014-10-14 2 views
0

tableView 행을 삭제하고 싶습니다. 데이터베이스에서 행을 삭제할 수 있지만 내 tableview의 행은 사라지지 않습니다. 감사합니다. !!! 는 MaytecommitEditingStyle : 데이터를 삭제하지만 행을 계속 표시합니다.

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

    if (editingStyle == UITableViewCellEditingStyleDelete) { 


     // Delete the row from the data source 
     PFUser *user = [self.allProducts objectAtIndex:indexPath.row]; 
     [_mipedido removeObject:[PFObject objectWithoutDataWithClassName:@"almacen"objectId:user.objectId]]; 
     [[PFUser currentUser] saveInBackground]; 


     NSMutableArray *pedido = [[NSMutableArray alloc] init]; 
     for (int i = 0; i <= 1; i++) 
     { 
      [pedido removeObject:[NSIndexPath indexPathForItem:i inSection:1]]; 
     } 

     [tableView beginUpdates]; 

     [tableView deleteRowsAtIndexPaths:pedido withRowAnimation:NO]; 

     [tableView endUpdates]; 
    } 

    [tableView reloadData]; 

} 

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

    static NSString *CellIdentifier [email protected]"editSnacksTableViewCell"; 
    editSnacksTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    if (cell == nil) { 
     NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"editSnacks" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 

    PFUser *user = [self.allProducts objectAtIndex:indexPath.row]; 

    cell.precio.text = [NSString stringWithFormat:@"%f",[[precioProducto text] doubleValue]]; 

    cell.nombreProducto.text =user[@"nombreProducto"]; 

// cell.photoSnacks.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]]; 
// cell.descripcionSnakcs.text =[tableData objectAtIndex:indexPath.row]; 

    return cell; 
} 

-(void)viewWillAppear:(BOOL)animated { 

    [super viewWillAppear:YES]; 

    self.mipedido = [[PFUser currentUser] objectForKey:@"mipedido"]; 

    PFQuery *query = [self.mipedido query]; 
    // [query whereKey:@"nombreProducto" notEqualTo:self.currentUser.username]; 
    [query orderByAscending:@"nombreProducto"]; 
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
     if (error) { 
      NSLog(@"Error %@ %@", error, [error userInfo]); 
     } 
     else { 
      self.allProducts =objects; 
      [self.tableView reloadData]; 
     } 
    }]; 

} 
+0

로 : 당신이 읽을 데이터베이스가 _mipedido 경우

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:NO]; 

전반적으로, 코드, 다음과 같아야 다음 indexPath에게 사용자를 삭제하기 위해

사용, 슬쩍 당신은 백그라운드에서 저장하고 있습니다, 그것은 당신이 데이터베이스를 다시 읽기 전에 저장할 시간이 없었을 수 있습니다. 동 기적으로 저장해보십시오. –

+0

지 점, 내 문제에 관심을 가져 주셔서 대단히 감사합니다. 사실, 나는 내 코드를 변경하기 위해 도달하고 마침내 나는 어떤 충돌도하지 않지만 나는 내가 최근에 삭제 한 행을 계속해서 보게된다. 상쾌하게하는 것 같아요. vviewWillAppear에서 Parse를 사용합니다. 이게이 문제의 원인인지 나는 모른다. 미리 감사드립니다. –

+0

인덱스 경로 객체가 'pedido'에 추가 된 위치는 어디입니까? –

답변

0

데이터를 다시로드하고 시작하고 업데이트가 deleteRowsAtIndexPaths 때문에 필요하지 않습니다 할 수있는 tableview의 호출 :이 부분을 처리합니다. 당신이 포함 pedido로 잘못된 인덱스 경로 (pedido)를 제거하기 위해 빈 시작 가변 배열을하지 않는 시도로

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

    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     PFUser *user = [self.allProducts objectAtIndex:indexPath.row]; 
     [_mipedido removeObject:[PFObject objectWithoutDataWithClassName:@"almacen"objectId:user.objectId]]; 
     [[PFUser currentUser] saveInBackground]; 


     NSMutableArray *pedido = [[NSMutableArray alloc] init]; 
     //Why use a loop? can't you just put 0 instead of i in the remove object? 
     for (int i = 0; i <= 1; i++) 
     { 
      //pedido is empty as it was just initiated. There is nothing to remove. 
      [pedido removeObject:[NSIndexPath indexPathForItem:i inSection:1]]; 
     } 
     //the NSMutableArray does not contain any index paths so it will not delete anything. 
     [tableView deleteRowsAtIndexPaths:pedido withRowAnimation:NO]; 
    } 
} 

, 데이터베이스에서 개체를 제거하지 않습니다 : 따라서 코드는 다음과 같이한다 모든 색인 경로. 따라서 tableView 행을 삭제하지 않습니다.

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

    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     PFUser *user = [self.allProducts objectAtIndex:indexPath.row]; 
     [_mipedido removeObject:[PFObject objectWithoutDataWithClassName:@"almacen"objectId:user.objectId]]; 
     [[PFUser currentUser] saveInBackground]; 
     //delete correct row from tableView 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:NO]; 
    } 
} 
+0

Ignacy. 다시 한번 나를 도우려는 것에 감사드립니다. 당신이 말한 모든 것에 당신이 옳습니다. 내가 말한대로 코드를 변경하지만 동일한 결과가 있습니다. 그래서 문제는 테이블 뷰 디스플레이를 새로 고치는 것입니다. 그리고 나는 아마 내가 쓰는 나의 viewWillAppear 메서드에있는 것이 무섭다. 나는 그 방법을 붙인다. –

+0

'- (void) viewWillAppear : (BOOL) animated { [super viewWillAppear : YES]; self.mipedido = [[PFUser currentUser] objectForKey : @ "mipedido"]; PFQuery * query = [self.mipedido query]; [query orderByAscending : @ "nombreProducto"]; [쿼리 findObjectsInBackgroundWithBlock : (NSArray * 개체, NSError * 오류) { if (error) { NSLog (@ "오류 % @ % @", 오류, [오류 userInfo]); } else { self.allProducts = objects; [self.tableView reloadData]; } }]; } ' –

+0

@maytelabarga가 tableview 데이터 소스 (cellForRowAtIndexPath)를 보는 것이 더 유용 할 것입니다. 질문에 추가 할 수 있습니까? –

관련 문제