2017-12-23 29 views
0

나는 질문을 게시 할 사용자가있는 내 앱이 있습니다. 사용자가 자신의 게시물을 삭제할 수있게 해달라고 부탁합니다. 여기 내 코드가있다.표보기 셀을 Swift에서 구문 분석을 사용하여 삭제

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath){ 
    if editingStyle == UITableViewCellEditingStyle.delete{ 
     print("here1") 
     let query = PFQuery(className: "Post") 
     query.whereKey("user", equalTo: PFUser.current()) 
     print("here2") 
     query.findObjectsInBackground(block: { (objects, error) in 
      if error != nil { 
       print("THERE WAS AN ERROR DELETING THE OBJECT") 
      }else{ 
       for object in objects!{ 
        print("here3") 
        object.deleteInBackground() 
        tableView.reloadData() 
       } 
      } 
     }) 
    } 
} 

예상대로 내 사용자 코드가 현재 사용자의 모든 게시물을 삭제했습니다. 선택한 게시물을 가져와 그 게시물 만 삭제하는 방법을 모르겠습니다. 여기 는 데이터베이스가

{ 
"_id": "EZY40tN2FR", 
"location": [ 
    -122.0312186, 
    37.33233141 
], 
"question": "Sldfksdlkfjsldfkjsldkfjsldkjf", 
"category": "Entertainment", 
"_p_user": "_User$R9GdCYnVno", 
"_created_at": { 
    "$date": "2017-10-17T01:03:11.438Z" 
}, 
"_updated_at": { 
    "$date": "2017-10-17T01:03:11.438Z" 
} 
} 
도와

감사를 구성하는 방법입니다! 데이터 가져 오기에 대한

코드 :

override func queryForTable() -> PFQuery<PFObject> { 
     let query = PFQuery(className: "Post") 
     query.includeKey("user") 
     query.whereKey("user", equalTo: PFUser.current()!) 
     query.order(byDescending: "createdAt") 
     query.limit = 100 
     return query 
    } 

답변

0

당신은 당신의 객체가이 방법으로 삭제 얻을 수 있습니다 :

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath){ 
    if editingStyle == UITableViewCellEditingStyle.delete{ 
     print("here1") 
     let objectToDelete = objects?[indexPath.row] as! PFObject 
     objectToDelete.deleteInBackgroundWithBlock { (success, error) in 
      if (success) { 
       // Force a reload of the table - fetching fresh data from Parse platform 
       self.loadObjects() 
      } else { 
       // There was a problem, check error.description 
      } 
     } 

    } 
} 
+0

가 어떻게이 yourObjectsArray을받을 수 있나요? –

+0

안녕하세요 @ RohanRao, "loadData"함수에서 findObjectsInBackground 쿼리를 사용하여 테이블 뷰를 채우고 질문을 업데이트하며 테이블 뷰 데이터 소스를 설정하는 방법을 보여줍니다. –

+0

안녕하세요, 최근 답변에 대해 죄송합니다. 사용자가 쿼리를 통해 작성한 모든 게시물을 가져 오는 중입니다. 내 코드를보고 모든 PFObject를 배열에 추가 할 수있는 위치를 알려주시겠습니까? 원래 게시물에 코드를 추가했습니다. 감사! –