[iOS] UITableView reload

Han's·2023년 8월 2일
0

TIL

목록 보기
11/20

UITableView 셀의 데이터가 바뀔 때 전체를 reload 하는 것보다 특정 인덱스를 reload 해주고 싶을 때가 있습니다. 이때 reloadRows를 사용하면 특정 index를 reload 할 수 있습니다.

  • indexPaths: 새로 고칠 셀의 인덱스 경로 배열입니다.
  • animation: 갱신할 때 셀에 적용할 애니메이션 옵션입니다.

func showScreenMode() {
        let alert = UIAlertController(title: "화면 모드", message: nil, preferredStyle: .actionSheet)
        let systemMode = UIAlertAction(title: "시스템 기본값", style: .default) { [weak self] _ in
            guard let self = self else { return }
            self.view.window?.overrideUserInterfaceStyle = .unspecified
            UserDefaults.standard.set(0, forKey: "Appearance")
            self.tableView.reloadRows(at: [IndexPath(row: 0, section: 1)], with: .automatic)
        }
        let lightMode = UIAlertAction(title: "라이트 모드", style: .default) { [weak self] _ in
            guard let self = self else { return }
            self.view.window?.overrideUserInterfaceStyle = .light
            UserDefaults.standard.set(1, forKey: "Appearance")
            self.tableView.reloadRows(at: [IndexPath(row: 0, section: 1)], with: .automatic)
        }
        let darkMode = UIAlertAction(title: "다크 모드", style: .default) { [weak self] _ in
            guard let self = self else { return }
            self.view.window?.overrideUserInterfaceStyle = .dark
            UserDefaults.standard.set(2, forKey: "Appearance")
            self.tableView.reloadRows(at: [IndexPath(row: 0, section: 1)], with: .automatic)
        }
        let cancel = UIAlertAction(title: "취소", style: .cancel)
        [systemMode, lightMode, darkMode, cancel].forEach {
            alert.addAction($0)
        }
        present(alert, animated: true)
    }

profile
 iOS Developer

0개의 댓글