【问题标题】:UITableView: how stop selection while swipe the table view cellUITableView:滑动表格视图单元格时如何停止选择
【发布时间】:2020-06-24 14:32:47
【问题描述】:

我在这里滑动删除代码,它是我的自定义 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 
}

该逻辑将根据选择进行表格视图展开折叠..但这里的问题是如果我滑动删除 setSelected 也会触发..不确定如何防止任何帮助将不胜感激..

【问题讨论】:

    标签: ios swift xcode uitableview


    【解决方案1】:

    在 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
    

    【讨论】:

      【解决方案2】:

      我不确定你想要什么样的外观,但这会产生相同的效果。

      如果在整个屏幕上滑动,则无需按下按钮即可触发删除。

      如果半滑动,您可以显示按钮供用户选择选项。

      半滑动(显示选项):

      完全滑动(触发删除按钮):

      尝试添加这两个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 方法,似乎两种方法都可以正常工作,但如果你选择这条路线,我认为你的方法没有必要

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2015-10-30
        • 2012-10-03
        • 1970-01-01
        • 2014-04-18
        • 1970-01-01
        • 1970-01-01
        • 2021-10-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多