简体   繁体   English

UITableView:如何在滑动表格视图单元格时停止选择

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

I have swipe to delete code here and it my custom TableViewCell I have implemented setSelected method like below..我在这里滑动删除代码,它是我的自定义 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 
}

The logic will do tableview expand collapse based on selection..but the problem here is if I swipe to delete setSelected also triggers.. not sure how to prevent that any help would be appreciated..逻辑将根据选择进行 tableview 扩展折叠..但这里的问题是如果我滑动删除 setSelected 也会触发..不知道如何防止任何帮助将不胜感激..

Try this in cellForRow在 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

I'm not sure what the appearance you are going for but this will produce the same effect.我不确定你想要什么样的外观,但这会产生相同的效果。

If swipe is across the whole screen it will trigger the delete without button press.如果在整个屏幕上滑动,它将触发删除而无需按下按钮。

If half swipe you can present buttons for user to choose options.如果半滑动,您可以显示按钮供用户选择选项。

Half swipe (Shows options):半滑动(显示选项):

在此处输入图像描述

Full swipe (Triggers delete button):完全滑动(触发删除按钮):

在此处输入图像描述

Try adding these two tableView delegate methods for a swipe to delete尝试添加这两个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]
        
}

I tried it with and without your tableView method and it seemed to work fine both ways but I don't think your method is necessary if you choose this route我尝试了使用和不使用您的 tableView 方法,并且两种方法似乎都可以正常工作,但是如果您选择此路线,我认为您的方法没有必要

Hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM