티스토리 뷰
iOS Swift + UIKit
iOS Swift 테이블뷰 삭제 버튼 이름 변경 (TableView delete button title change)
GonsLab 2021. 1. 22. 09:29반응형
UITableView 를 Swipe 했을 때 나오는 삭제 버튼명을 변경해보겠습니다.
기본은 Delete 로 나옵니다.
UITableViewDelegate 를 상속 받으신 후에 진행해야합니다.
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
return "삭제"
}
이 코드를 작성하면 됩니다.
잘 변경됐네요.
전체 코드입니다.
import UIKit
class ViewController3: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var myTableView: UITableView!
var dataArray: Array<String> = ["TableView row 0","TableView row 1","TableView row 2"]
override func viewDidLoad() {
super.viewDidLoad()
myTableView.delegate = self
myTableView.dataSource = self
}
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 {
}
}
//테이블 삭제 코멘트 Delete에서 삭제로 바꾸기
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
return "삭제"
}
}
반응형
'iOS Swift + UIKit' 카테고리의 다른 글
iOS Swift UIContextMenuInteraction 꾹 눌러서 나오는 메뉴 만들기 (Long press menu tableview) (0) | 2021.01.26 |
---|---|
iOS Swift 테이블뷰 스와이프 버튼 만들기 (TableView swipe button) (0) | 2021.01.25 |
iOS Swift 테이블뷰 스와이프 삭제 (TableView swipe delete) (0) | 2021.01.21 |
iOS Swift 테이블뷰 라인 없애기 (TableView remove line) (0) | 2021.01.20 |
iOS Swift 테이블뷰 선택 색상 없애기 (TableView seleted color delete) (0) | 2021.01.19 |
댓글