2017-11-04 9 views
1

에 의해 행을 삭제하는 것 같다. UIContextualAction은 제가 보는 문제는 내가 <code>UIContextualAction</code><code>completionHandler</code>에서 통과 <code>true</code><strong><code>.destructive</code>로하고</strong>을 만들 때 열을 제거하기위한 기본 동작이있을 것이다 기본

당신이 엑스 코드의 템플릿에서 새 마스터 - 세부 응용 프로그램을 작성하고 MasterViewController에이 코드를 추가하는 경우

...

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { 
    let testAction = UIContextualAction(style: .destructive, title: "Test") { (_, _, completionHandler) in 
     print("test") 
     completionHandler(true) 
    } 
    return UISwipeActionsConfiguration(actions: [testAction]) 
} 

는 당신이 슬쩍 행이 제거됩니다. 테이블 뷰를 업데이트하는 코드가 없음을 주목하십시오. 또한 모델이 업데이트되지 않으며 행을 다시로드하도록 스크롤하면 다시 표시됩니다.

이 경우 false을 전달해도 행이 제거되지 않습니다. 또는 .normal 스타일과 true을 사용하면 행을 제거하지 않습니다.

.destructivetrue은 행이 기본적으로 제거되도록합니다.

누구든지이 동작을 설명 할 수 있습니까? 행이 삭제되는 이유는 무엇입니까? 파괴적인 옵션에 대한 문서 당

답변

0

: 데이터를 삭제하거나 파괴적인 작업의 몇 가지 유형을 수행

액션입니다.

완료 란 작업이 성공했음을 나타냅니다. 진실을 전달한다는 것은 파괴적인 임무가 성공적 이었기 때문에 행을 제거해야한다는 것을 의미합니다.

파괴적인 행동이 발생했을 때 수동으로 데이터 소스를 업데이트해야하며 그렇게하지 않으면 스크롤을 통해 데이터가 다시 나타납니다. 또한 데이터가 삭제되었음을 tableView에 알려줘야합니다. 조금 나에게 혼란 유일한 부분이있는 tableview를 다시로드하거나 indexPath을 위해 DeleteRows를 호출 할 필요가있다

UIContextualAction(style: .destructive, title: "Delete") { [weak self] (_, _, completion) in 
    if self?.canDelete(indexPath) { // We can actually delete 
     // remove the object from the data source 
     self?.myData.remove(at: indexPath.row) 
     // delete the row. Without deleting the row or reloading the 
     // tableview, the index will be off in future swipes 
     self?.tableView?.deleteRows(at: [indexPath], with: .none) 
     // Let the action know it was a success. In this case the 
     // tableview will animate the cell removal with the swipe 
     completion(true)  
    } else { // We can't delete for some reason 
     // This resets the swipe state and nothing is removed from 
     // the screen visually. 
     completion(false) 
    }   
} 

제대로에 계산

: 아래

는 일 예를 나타내는 몇 가지 코드 다음 번 스 와이프.

내가 10 행을 삭제하려면 5 번째를 스 와이프하면 tableview가 다시로드되거나 테이블 뷰에서 행이 제거되는 경우를 제외하고 그 이후의 모든 사용자는 한 행을 벗어납니다.