簡體   English   中英

UITableView:如何在滑動表格視圖單元格時停止選擇

[英]UITableView: how stop selection while swipe the table view cell

我在這里滑動刪除代碼,它是我的自定義 TableViewCell 我已經實現了 setSelected 方法,如下所示..

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
       // tableView.allowsSelectionDuringEditing = false
        if tableView.indexPathForSelectedRow != nil, tableView.indexPathForSelectedRow == indexPath {
            return UITableViewCell.EditingStyle.none
        }
        return UITableViewCell.EditingStyle.delete
    }
override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        //some code here 
}

邏輯將根據選擇進行 tableview 擴展折疊..但這里的問題是如果我滑動刪除 setSelected 也會觸發..不知道如何防止任何幫助將不勝感激..

在 cellForRow 中試試這個

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell_identifier", for: indexPath)                
    cell.selectionStyle = UITableViewCell.SelectionStyle.none
    //or this based on swift version 
    cell.selectionStyle = .none
    return cell

我不確定你想要什么樣的外觀,但這會產生相同的效果。

如果在整個屏幕上滑動,它將觸發刪除而無需按下按鈕。

如果半滑動,您可以顯示按鈕供用戶選擇選項。

半滑動(顯示選項):

在此處輸入圖像描述

完全滑動(觸發刪除按鈕):

在此處輸入圖像描述

嘗試添加這兩個tableView委托方法以進行滑動刪除

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    // Determine which rows should be editable
    return true
}
    
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: 
    IndexPath) -> [UITableViewRowAction]? {
        
    let button1 = UITableViewRowAction(style: .default, title: "Delete") { 
        action, indexPath in
        print("delete pressed")
        // Consider alert to confirm that it was intentionally deleted
    }
    button1.backgroundColor = UIColor.red

    // Create any buttons you want

    return [button1]
        
}

我嘗試了使用和不使用您的 tableView 方法,並且兩種方法似乎都可以正常工作,但是如果您選擇此路線,我認為您的方法沒有必要

希望這可以幫助

暫無
暫無

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

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