본문 바로가기
iOS, Swift 개발

Swift UITableView의 리스트 Value 변경 방법

by Nin J 2024. 4. 16.

tableView의 리스트의 값을 추가, 삭제 등등 변경 하고 싶을때 사용한다.

1, 2 번 두가지의 방법이 있고  

1번 방법은 iOS 11부터 지원하는 새로운 방식이다. 2번의 경우 아직 deprecated 되지 않았으나 나중을 위해 1번 방식으로 사용을 추천.

1. performBatchUpdates({  })

UICollectionView 또는 UITableView에서 여러 업데이트 작업을 일괄적으로 처리할 때 사용된다. 이 메서드는 beginUpdates()endUpdates()를 직접 호출하지 않고, 대신에 클로저 내에서 여러 업데이트 작업을 수행한다.

일반적으로 이 메서드는 여러 셀이나 섹션을 추가, 삭제, 이동하는 등의 작업을 처리할 때 사용된다. 예를 들어, 여러 셀을 한 번에 추가하고자 할 때 다음과 같이 사용할 수 있다:

이 메서드의 클로저 내에서 수행되는 모든 작업은 단일 배치 업데이트로 간주되어, 한 번의 UI 업데이트로 처리된다. 이는 사용자에게 부드러운 애니메이션을 제공하고, UI 업데이트가 한꺼번에 발생함으로써 성능을 향상시킨다.

performBatchUpdates(_:) 메서드의 completion 핸들러를 활용하면, 업데이트 작업이 완료된 후에 추가적인 작업을 수행할 수 있다. 일반적으로는 업데이트 작업과 관련된 애니메이션 및 레이아웃 업데이트가 완료된 후에 수행된다.

example:

tableView.performBatchUpdates({ 

//Somgthing. example

  self.dataList.append(item)                                           

  KongPlayerManager.sharedInstance.arrPlayListAod.append(item)

  let newIndexPath = IndexPath(row: self.dataList.count - 1, section: 0)

  tableView.insertRows(at: [newIndexPath], with: .bottom)

})

https://developer.apple.com/documentation/uikit/uicollectionview/1618045-performbatchupdates

 

performBatchUpdates(_:completion:) | Apple Developer Documentation

Animates multiple insert, delete, reload, and move operations as a group.

developer.apple.com

 

2. beginUpdates(), endUpdates()

beginUpdates()endUpdates()는 UITableView나 UICollectionView에서 사용되는 메서드이다. 이들은 여러 행(row)이나 섹션(section)을 동시에 업데이트할 때 사용되며, 업데이트가 일어나는 동안에 테이블이나 컬렉션 뷰의 레이아웃이 한 번만 업데이트되도록 보장한다.

일반적으로 다음과 같은 상황에서 이들 메서드를 사용한다:

  • 여러 행이나 섹션을 추가, 삭제, 업데이트하는 경우
  • 여러 행이나 섹션의 내용을 일괄적으로 변경하는 경우
  • 여러 행이나 섹션을 재배치하는 경우

예를 들어, 테이블 뷰에서 여러 행을 추가하고자 할 때는 다음과 같이 사용할 수 있다:

beginUpdates()endUpdates() 사이에서 삽입, 삭제, 이동 등의 작업을 수행하면, 이들 메서드는 테이블 뷰의 레이아웃을 한 번만 업데이트하여 성능을 향상시킵니다. 이렇게 함으로써 애니메이션 효과도 부드럽게 보여줄 수 있다.

그러나 주의할 점은 beginUpdates()endUpdates()를 짝을 이루어 사용해야 하며, 그 사이에서 단일 업데이트 블록 안에서만 작업해야 합니다. 이들 메서드를 사용하지 않고 단일 작업을 진행할 때에는 일반적으로 reloadData()를 사용하는 것이 좋다.

example:

tableView.beginUpdates()

//Somgthing. example

  self.dataList.append(item)                                           

  KongPlayerManager.sharedInstance.arrPlayListAod.append(item)

  let newIndexPath = IndexPath(row: self.dataList.count - 1, section: 0)

  tableView.insertRows(at: [newIndexPath], with: .bottom)

 tableView.endUpdates()

https://developer.apple.com/documentation/uikit/uitableview/1614908-beginupdates

 

beginUpdates() | Apple Developer Documentation

Begins a series of method calls that insert, delete, or select rows and sections of the table view.

developer.apple.com

 

또한 테이블뷰의 리스트의 가장 처음(배열 0번째) 가장 마지막 (배열 마지막 번째)의 이벤트를 알고 싶으면 

UITableViewDelegate의 

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {}

사용하면 해당 인덱스를 체크 후 해당 리스트의 처음 과 마지막 이벤트를 받을수 있다.

처음(배열 0번째) = if indexPath.row  == 0 {}

마지막 (배열 마지막 번째)if indexPath.row == self.dataList.count - 1{}