簡體   English   中英

如何通過滑動操作設置 UITableView 編輯模式?

[英]How to set a UITableView Editing Mode from a swipe action?

我需要通過單擊“移動”滑動操作中的其中一個單元格來打開 tableView 編輯模式:

滑動動作代碼:

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let move = UIContextualAction(style: .normal, title: "Переместить") { (action, view, completionHandler) in
        self.turnEditing()
        completionHandler(true)
    }
    move.backgroundColor = UIColor(named: "CalmBlueColor")
    
    let configuration = UISwipeActionsConfiguration(actions: [move])
    return configuration
}

轉編輯()function:

   func turnEditing() {
       if tableView.isEditing {
           tableView.isEditing = false
       } else {
           tableView.isEditing = true
       }
    }

表視圖代表:

 override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
    return true
}

override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    
}

override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
    return .none
}

override func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
    return false
}

當我按下滑動操作時,它只是關閉,沒有進入編輯模式...這是GIF

go 是否可以通過滑動操作或僅通過 barButtonItem + IBAction 進入編輯模式?

添加一些延遲

DispatchQueue.main.asyncAfter(deadline: .now() + 0.75) {
  self.turnEditing()
}

順便說一句,你可以替換這個

if tableView.isEditing {
  tableView.isEditing = false
} else {
  tableView.isEditing = true
}

tableView.isEditing.toggle()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM