Closed3

【Swift】NavigationItem.rightBarButtonItem 0->1

yoshitakayoshitaka
 self.navigationItem.rightBarButtonItem = self.editButtonItem

これでedit <-> doneボタンが実装される
☝️storyboardでbarButtonを置いても無視される
当然だが、上の実装をコメントアウトするとstoryboardに置いたボタンが表示された
つまり、viewDidLoadでボタンが上書きされた感じか

tableViewControllerには標準で実装されている

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }

でテーブルが自動でdeleteできる感じになる

yoshitakayoshitaka

editとdoneの名前変えるのはこれでいける!

override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
        if self.isEditing {
            self.editButtonItem.title = "選択"
        } else {
            self.editButtonItem.title = "完了"
        }
    }

ただ、初期表示で変わらないので、

 self.navigationItem.rightBarButtonItem?.title = "選択"

でいけた

ちなみにこれはUIViewControllerから

yoshitakayoshitaka

UIBarButtonItem.Style

enum Style : Int
case plain = 0
//タップすると光ります。 デフォルトのアイテムスタイル。
case done = 2
//完了ボタンのスタイル-たとえば、いくつかのタスクを完了して前のビューに戻るボタン。
このスクラップは2021/05/05にクローズされました