Nice programing

'잘못된 업데이트 : 섹션 0의 잘못된 행 수

nicepro 2020. 11. 26. 19:53
반응형

'잘못된 업데이트 : 섹션 0의 잘못된 행 수


이와 관련된 모든 관련 게시물을 읽었으며 여전히 오류가 있습니다.

'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (5), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

세부 사항은 다음과 같습니다.

.h나는이 NSMutableArray:

@property (strong,nonatomic) NSMutableArray *currentCart;

에서 .mnumberOfRowsInSection다음과 같다 :

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.


    return ([currentCart count]);

}

삭제를 활성화하고 어레이에서 객체를 제거하려면 :

// Editing of rows is enabled
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {

        //when delete is tapped
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        [currentCart removeObjectAtIndex:indexPath.row];


    }
}

나는 내가 편집하는 배열의 수에 의존하는 내 섹션 수를 가지면 적절한 행 수를 보장 할 것이라고 생각했다. 어쨌든 행을 삭제할 때 테이블을 다시로드하지 않고이 작업을 수행 할 수 없습니까?


를 호출 하기 전에 데이터 배열에서 객체를 제거해야합니다 deleteRowsAtIndexPaths:withRowAnimation:. 따라서 코드는 다음과 같아야합니다.

// Editing of rows is enabled
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {

        //when delete is tapped
        [currentCart removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

배열 생성 바로 가기를 사용하여 코드를 약간 단순화 할 수도 있습니다 @[].

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

Swift Version-> 호출하기 전에 데이터 배열에서 객체를 제거하십시오.

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        print("Deleted")

        currentCart.remove(at: indexPath.row) //Remove element from your array 
        self.tableView.deleteRows(at: [indexPath], with: .automatic)
    }
}

내 경우 문제는 numberOfRowsInSection호출 후 비슷한 수의 행을 반환하는 것 tableView.deleteRows(...)입니다.

이것이 필자의 경우 필수 동작 이었기 때문에 행을 삭제 한 후에도 동일하게 유지 되는 경우 tableView.reloadData()대신 전화 걸었습니다 .tableView.deleteRows(...)numberOfRowsInSection

참고 URL : https://stackoverflow.com/questions/21870680/invalid-update-invalid-number-of-rows-in-section-0

반응형