簡體   English   中英

SwipeActions 在 tableView swift 5 xcode 13.4.1 中不起作用

[英]SwipeActions does not work in tableView swift 5 xcode 13.4.1

我正在對我的 tableview 進行 trailingSwipe,但它不起作用。 不調用 function trailingSwipeActionsConfigurationForRowAt

嘗試使用leadingSwipeActionsConfigurationForRowAt ,如果它對我有用,但不適用於trailingSwipe

有效,我有委托和數據源表視圖

代碼

 func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { let delete = UIContextualAction(style: .normal, title: "delete2") {(action, view, completionHandler) in print("delete2 \(indexPath.row)") } let swipe = UISwipeActionsConfiguration(actions: [delete]) return swipe }

使用 Apple Mac M1 ( MacBook Pro M1)

我也遇到了這個問題。 也許這個問題與Apple Mac M1有關,請嘗試使用Apple intel

請檢查這個問題

下面的代碼在我的 Apple Intel 中運行良好。

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let action = UIContextualAction(style: .destructive, title: "REMOVE") { [self] action, view, completion in
            // Your swipe action code!
            completion(true)
        }
    
    action.backgroundColor = UIColor.red
    
    let swipeAction = UISwipeActionsConfiguration(actions: [action])
    swipeAction.performsFirstActionWithFullSwipe = false        // Full swipe disable
    
    return swipeAction
}

試試這個,在 Xcode 13+、Apple Mac M1 和 Swift 5 上運行良好

 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
} 
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {}

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let delete = UIContextualAction(style: .destructive, title: "Delete") { (action, sourceView, completionHandler) in
      //  print("index path of delete: \(indexPath)")
        
        completionHandler(true)
    }
    let edit = UIContextualAction(style: .normal, title: "Edit") { [weak self] (action, sourceView, completionHandler)  in
        
        completionHandler(true)
    }
    delete.title = ""
    delete.image = UIImage(named: "deleteProfile")
    
    edit.title = ""
    edit.image = UIImage(named: "editProfile")
    
    let swipeAction = UISwipeActionsConfiguration(actions: [delete,edit])
    swipeAction.performsFirstActionWithFullSwipe = false // This is the line which disables full swipe
    return swipeAction
}

暫無
暫無

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

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