[ios] UITableView는 스 와이프하여 삭제할 수 없지만 여전히 편집 모드에서 삭제가 있습니까?

행을 스 와이프하여 삭제할 수 없지만 편집 모드에서 행을 삭제할 수있는 Alarm 앱과 비슷한 것을 원합니다.

tableView : commitEditingStyle : forRowAtIndexPath :를 주석 처리 할 때 스 와이프하여 삭제를 비활성화하고 편집 모드에서 삭제 버튼을 계속 사용했지만 삭제 버튼을 누르면 어떻게됩니까? 뭐라고 불러?



답변

좋아, 그것은 매우 쉬운 것으로 판명되었습니다. 이것이 내가 이것을 해결하기 위해 한 일입니다.

목표 -C

- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Detemine if it's in editing mode
    if (self.tableView.editing)
    {
        return UITableViewCellEditingStyleDelete;
    }

    return UITableViewCellEditingStyleNone;
}

스위프트 2

override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
    if tableView.editing {
         return .Delete
    }

    return .None
}

스위프트 3

override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    if tableView.isEditing {
        return .delete
    }

    return .none
}

tableView:commitEditingStyle:forRowAtIndexPath:삭제를 커밋하려면 여전히 구현해야합니다 .


답변

명확하게하기 위해 스 와이프하여 삭제 tableView:commitEditingStyle:forRowAtIndexPath:는 구현 되지 않는 한 활성화되지 않습니다 .

개발 중에는 구현하지 않았으므로 스 와이프하여 삭제 기능이 활성화되지 않았습니다. 물론 완성 된 앱에서는 항상 구현됩니다. 그렇지 않으면 편집 할 필요가 없기 때문입니다.


답변

Swift 버전 :

override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {

    if(do something){

        return UITableViewCellEditingStyle.Delete or UITableViewCellEditingStyle.Insert

    }
    return UITableViewCellEditingStyle.None

}


답변

CanEditRowAt 함수를 구현해야합니다.

EditingStyleForRowAt 함수에서 .delete를 반환 할 수 있으므로 편집 모드에서 계속 삭제할 수 있습니다.

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    if tableView.isEditing {
        return true
    }
    return false
}

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
    return .delete
}


답변

기본적으로 방법을 사용하여 편집을 활성화 또는 비활성화합니다.

- (void)setEditing:(BOOL)editing animated:(BOOL)animated

편집이 활성화 된 경우 빨간색 삭제 아이콘이 나타나고 사용자에게 삭제 확인이 요청됩니다. 사용자가 확인하면 위임 방법

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

삭제 요청을 알립니다. 이 방법을 구현하면 스 와이프하여 삭제가 자동으로 활성화됩니다. 이 방법을 구현하지 않으면 스 와이프하여 삭제가 활성화되지 않지만 실제로 행을 삭제할 수는 없습니다. 따라서 내가 아는 한 문서화되지 않은 비공개 API를 사용하지 않으면 요청한 것을 얻을 수 없습니다. 아마도 이것이 Apple 애플리케이션이 구현되는 방법입니다.


답변

C #에서 :

스 와이프에서 삭제 옵션으로 행을 활성화 / 비활성화해야하는 동일한 문제가 발생했습니다. 여러 행을 왼쪽으로 스 와이프하고 삭제해야하며 다른 색상으로 유지해야합니다. 이 논리를 사용하여 달성했습니다.

[Export("tableView:canEditRowAtIndexPath:")]
public bool CanEditRow(UITableView tableView, NSIndexPath indexPath)
{

    if (deletedIndexes.Contains(indexPath.Row)){
        return false;
    }
    else{
        return true;
    }

}

deletedIndexes는 테이블에서 중복없이 삭제 된 인덱스 목록입니다. 이 코드는 행이 삭제되었는지 확인한 다음 스 와이프를 비활성화하거나 그 반대의 경우도 마찬가지입니다.

동등한 위임 함수는 Swift is canEditRowAtIndexPath입니다.


답변

나는이 문제를 발견하고 아래 코드로 수정했습니다. 도움이되기를 바랍니다.

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

    BOOL deleteBySwipe = NO;
    for (UIGestureRecognizer* g in tableView.gestureRecognizers) {
        if (g.state == UIGestureRecognizerStateEnded) {
            deleteBySwipe = YES;
            break;
        }
    }

    if (deleteBySwipe) {
        //this gesture may cause delete unintendedly
        return;
    }
    //do delete
}

}