티스토리 뷰
반응형
UITableView 를 Swipe 해서 cell 을 remove 해보겠습니다.
코드로 작성해야합니다.
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
dataArray.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
}
}
이렇게 작성하면 끝입니다.
잘 나오네요.
전체 코드입니다.
import UIKit
class ViewController3: UIViewController, UITableViewDataSource {
@IBOutlet var myTableView: UITableView!
var dataArray: Array<String> = ["TableView row 0","TableView row 1","TableView row 2"]
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = dataArray[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
dataArray.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
}
}
}
반응형
'iOS Swift + UIKit' 카테고리의 다른 글
iOS Swift 테이블뷰 스와이프 버튼 만들기 (TableView swipe button) (0) | 2021.01.25 |
---|---|
iOS Swift 테이블뷰 삭제 버튼 이름 변경 (TableView delete button title change) (0) | 2021.01.22 |
iOS Swift 테이블뷰 라인 없애기 (TableView remove line) (0) | 2021.01.20 |
iOS Swift 테이블뷰 선택 색상 없애기 (TableView seleted color delete) (0) | 2021.01.19 |
iOS Swift 화면 안꺼지게 하는 코드, 자동 잠금 방지 (Screen lock disable) 절전모드 (2) | 2021.01.15 |
댓글