일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
Tags
- 로그 찍기
- Business Manager
- print vs dump
- color 변경
- archive error
- 테이블뷰 밀어서 삭제
- 로그출력
- MVVM
- 테이블 뷰
- Apple Enterprise
- 화면 가리기
- AppleLogin
- RxSwift
- 스토리보드 없이
- 하단 네비게이션바
- snapkit
- 비공개 앱 배포
- failed: No such file or directory (2)
- navigationbar
- testflight error
- reactorkit
- RxCocoa
- 소셜로그인
- 아카이브 에러
- codebase
- iOS 광고달기
- CryptoSwift
- then
- 백그라운드 진입시 화면
- 테이블뷰 셀 밀어서 삭제
Archives
- Today
- Total
ukSeung iOS
[Swift] UITableView Cell 밀어서 삭제 본문
안녕하세요 욱승임다 ㅋ
좋은 주말보내고 계신가요? ㅎㅎ
이번포스팅에서는 UITableView Cell을 밀어서 삭제하는 동작을 구현해볼게요
예제
예제 코드
import Foundation
import UIKit
import SnapKit
class SwipeToDeleteCellViewController: UIViewController, UIViewControllerAttribute {
var navTitle: String?
var lastContentOffset: CGFloat = 0.0
var tableViewArray: [Int] = []
lazy var tableView = UITableView().then {
$0.delegate = self
$0.dataSource = self
$0.register(TitleTableViewCell.self, forCellReuseIdentifier: "TitleTableViewCell")
}
override func viewDidLoad() {
super.viewDidLoad()
setTableViewArray()
setNavigationBar()
setUI()
setAttributes()
bindRx() // 미사용 함수
}
func setTableViewArray() {
for x in 0...100 {
tableViewArray.append(x)
}
}
func setNavigationBar() {
self.navigationItem.title = navTitle ?? ""
}
func setUI() {
self.view.backgroundColor = .white
self.view.addSubview(tableView)
}
func setAttributes() {
tableView.snp.makeConstraints {
$0.edges.equalTo(self.view.safeAreaLayoutGuide)
}
}
/// 미사용 함수
func bindRx() {
}
}
extension SwipeToDeleteCellViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableViewArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TitleTableViewCell", for: indexPath) as! TitleTableViewCell
cell.title.text = String(tableViewArray[indexPath.row])
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
tableView.beginUpdates() // beginUpdate
tableView.deleteRows(at: [indexPath], with: .fade) // 셀 삭제 애니메이션 설정
tableViewArray.remove(at: indexPath.row) // 데이터 소스에서 해당 셀의 데이터 삭제
tableView.endUpdates() // endUpdate
}
}
}
예제로 0부터 100까지 숫자를 배열에 append 하여 예제 코드를 짰슴
중요한 부분은 맨밑 tableViwe commit
.delete 될때 하는 동작들을 유심히 봐둘것!
결론
포스팅 길이가 짧은만큼 간단하게 구현 가능한 기능! 🤭
GitHub
GitHub - shinseunguk/SwiftPractice: SwtiftPractice
SwtiftPractice. Contribute to shinseunguk/SwiftPractice development by creating an account on GitHub.
github.com
728x90
반응형
'iOS > Swift' 카테고리의 다른 글
[Swift] Hotspot Configuration, 핫스팟 연결 (1) | 2023.05.24 |
---|---|
[Swift] RxSwift+MVVM, Sign In with Apple 애플 로그인 (0) | 2023.05.23 |
[Swift] ScrollView 스크롤 네비게이션바 hide On/Off (1) | 2023.05.19 |
[Swift] UITabbar, 하단 탭바 구현 (0) | 2023.04.30 |
[iOS] Background, Inactive(Foreground) 진입시 화면가리기 (0) | 2023.03.23 |