iOS Swift tableView 編輯模式 左滑 刪除數(shù)據(jù)

編輯模式

1. editButtonItem

  self.navigationItem.rightBarButtonItem = editButtonItem
//配合editButtonItem
    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
        self.tableView.setEditing(editing, animated: true)
    }

2.左滑刪除

直接實(shí)現(xiàn)下面的方法

實(shí)現(xiàn)的方法

  //設(shè)置哪些行可以編輯
  func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        if(indexPath.row == 0){
            return false
        }else{
            return true
        }
    }
    
    // 設(shè)置單元格的編輯的樣式
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return UITableViewCell.EditingStyle.delete
    }
    
    //設(shè)置點(diǎn)擊刪除之后的操作
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            workManager.updateCollection(withYear: yearForSearch, month: monthForSearch, row: indexPath.row - 1)
            count = count - 1
            tableView.deleteRows(at: [indexPath], with: .fade)
            tableView.reloadRows(at: [IndexPath(row: 0, section: 0)], 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
        }
    }

其中count - 1是因?yàn)閚umberOfRowsInSection這里返回了count,必須要count - 1 否則會報(bào)錯Invalid update,意思是刪除前后 數(shù)量應(yīng)該對應(yīng),count不減1的話數(shù)量不對。

其它方法

多個按鈕

editActionsForRowAtIndexPath
能設(shè)置出現(xiàn)編輯的按鈕 默認(rèn)是刪除 可以自定義UITableViewRowAction
可以設(shè)置多個操作,操作完tableView.editing = False 退出編輯模式

多選

allowsMultipleSelectionDuringEditing

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、簡介 <<UITableView(或簡單地說,表視圖)的一個實(shí)例是用于顯示和編輯分層列出的信息的一種手段 <<...
    無邪8閱讀 10,958評論 3 3
  • 一.準(zhǔn)備工作 申請一個 SSL 證書 ,可在沃通WoSign網(wǎng)站申請所需SSL證書類型。SSL證書按驗(yàn)證的類別可分...
    南南總是懶懶閱讀 850評論 0 4
  • https://github.com/potato512/SYSwiftLearning override fun...
    小緈福閱讀 2,215評論 0 0
  • *7月8日上午 N:Block :跟一個函數(shù)塊差不多,會對里面所有的內(nèi)容的引用計(jì)數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,711評論 1 14
  • 只有通過科學(xué)的方法,大量訓(xùn)練邏輯思考能力并且運(yùn)用它,才能在這個快速變化的時代中立于不敗之地 一套結(jié)論可以反復(fù)套用的...
    青青狐閱讀 696評論 0 0

友情鏈接更多精彩內(nèi)容