[iOS] UITableView drag&drop 버그

Charlie·2022년 11월 15일

아직 해결못함..

지난 글에서 UITableView에서 셀을 드래그해서 data source를 수정하는 방법에 대해서 알아봤었어요.

하지만.. 이상한 버그가 일어나서 버그 해결을 하고 가도록 하겠습니다.

문제 상황

Data Source, cellForRow

보시는 것 처럼 시간차이를 두고 update가 되죠..? ㅠㅠ
문제 상황을 자세히 보면 드래그 되는 친구 말고는 바로 바뀌는걸 볼 수 있는데, 드래그가 되는 친구는 시간이 지나고 업데이트 되는 것을 알 수 있어요.


그래서 혹시나 Diffable Data Source를 사용해보기로 했어요

Diffable Data Source

하지만 얘는 무슨 도박도 아니고 됐다가 안됐다가...🤪


snapshot을 apply하는 곳이랑 dataSource 메소드에 각각 프린트문을 찍어봤을 때 실제로 결과가 dataSource 메소드가 실행될 때도 있고, 그렇지 않을 때도 있어요.. 왜이러는걸까요?

override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        guard let fromItem = itemIdentifier(for: sourceIndexPath),
              let toItem = itemIdentifier(for: destinationIndexPath),
              sourceIndexPath != destinationIndexPath else { return }
        
        var snap = snapshot()
        snap.deleteItems([fromItem])
        
        if destinationIndexPath.row > sourceIndexPath.row {
            snap.insertItems([fromItem], afterItem: toItem)
        } else {
            snap.insertItems([fromItem], beforeItem: toItem)
        }
        apply(snap, animatingDifferences: true)
        print("✨✨✨✨✨apply")
}
private func setPlaceListTableDataSource() {
        self.dataSource = PlaceListTableDataSource(tableView: self.placeListView.mapPlaceTableView) { tableView, indexPath, place -> UITableViewCell? in
            guard let cell = tableView.dequeueReusableCell(withIdentifier: MapPlaceTableViewCell.identifier, for: indexPath) as? MapPlaceTableViewCell else { return nil }
            
            print("🔥🔥🔥🔥🔥cell configure")
            cell.configure(number: indexPath.row + 1, place: place)
            cell.deleteButton.tag = indexPath.row
            cell.deleteButton.addTarget(self, action: #selector(self.didTapDeleteButton(_:)), for: .touchUpInside)
            
            return cell
        }
}
// 실행 결과
✨✨✨✨✨apply	// 1번째 -> 안됨..
✨✨✨✨✨apply	// 2번째 -> 안됨..
✨✨✨✨✨apply	// 3번째 -> 얘는 됨..
🔥🔥🔥🔥🔥cell configure
🔥🔥🔥🔥🔥cell configure
🔥🔥🔥🔥🔥cell configure
profile
Hello

0개의 댓글