iOS/Swift

[Swift] UITableView, reloadData(), deleteData() / Swipe Delete

YEN_ 2023. 12. 14. 21:43

 

UITableView

https://developer.apple.com/documentation/uikit/uitableview

 

UITableView | Apple Developer Documentation

A view that presents data using rows in a single column.

developer.apple.com

 

단일 열의 세로행을 사용해서 데이터를 표시하는 뷰

각 행에는 1개의 콘텐츠가 포함된다

 

실제 콘텐츠를 표시하는 Cell을 제공한다 -> UITableViewCell

 

 

 

reloadData()

https://developer.apple.com/documentation/uikit/uitableview/1614862-reloaddata

 

reloadData() | Apple Developer Documentation

Reloads the rows and sections of the table view.

developer.apple.com

 

테이블 뷰의 보이는 영역 전체를 업데이트 할 때 주로 사용

 

func reloadData()

 

 

 

deleteRows()

https://developer.apple.com/documentation/uikit/uitableview/1614960-deleterows

 

deleteRows(at:with:) | Apple Developer Documentation

Deletes the rows that an array of index paths identifies, with an option to animate the deletion.

developer.apple.com

 

table view cell을 삭제할 때 애니메이션을 적용하는 옵션을 사용할 수 있다

인덱스 경로 배열([indexPath])를 식별하여 해당하는 행을 삭제한다

 

func deleteRows(
    at indexPaths: [IndexPath],
    with animation: UITableView.RowAnimation
)

 

 

 

 

 

 

Swipe Delete

이런 거

 

UITableViewDataSource

이 두가지를 추가해서 구현해주면 된다.

 

tableView(_:canEditRowAt:)

optional func tableView(
    _ tableView: UITableView,
    canEditRowAt indexPath: IndexPath
) -> Bool

주어진 행이 편집 가능한지 확인할 수 있게 요청.

 

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

 

 

 

tableView(_:commit:forRowAt:)

optional func tableView(
    _ tableView: UITableView,
    commit editingStyle: UITableViewCell.EditingStyle,
    forRowAt indexPath: IndexPath
)

지정된 행의 삽입/삭제를 하도록 데이터 소스에 요청

 

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            self.testData.remove(at: indexPath.row)
            TodoListTableView.deleteRows(at: [indexPath], with: .fade)
        }
    }