简体   繁体   English

Swift:如何防止同时擦拭两个表格单元格?

[英]Swift: How to prevent two table cells to be swiped at the same time?

I have a table view with cells that the user can swipe to delete the row. 我有一个表格视图,其中的单元格可供用户滑动以删除该行。 I'm receiving a fatal error: Index out of range error whenever I swipe two rows at once (with two fingers, one on each row). 我收到一个fatal error: Index out of range每当我一次滑动两行(用两根手指,每行一根手指)时, fatal error: Index out of range错误。

I've added these 4 lines in my OrdersViewController : 我在OrdersViewController添加了这4行:

  override func viewDidLoad() {
    ...
    self.view.isMultipleTouchEnabled = false
    self.view.isExclusiveTouch = true
    self.ordersTable.isMultipleTouchEnabled = false
    self.ordersTable.isExclusiveTouch = true
  }

in an effort to try to change this behavior so that only one cell is swiped at a time, but nothing has changed. 尝试更改此行为,以便一次只刷一个单元格,但没有任何变化。 I can still use two fingers on two different rows to swipe back and forth at the same time. 我仍然可以在两个不同的行上使用两个手指同时来回滑动。

Is there another way? 还有另一种方法吗?

None of the IsExclusiveTouch and IsMultipleTouchEnabled is working regardless of where I put it. 不管我放在哪里,IsExclusiveTouch和IsMultipleTouchEnabled都不起作用。 I wonder if it has anything to do with me using this library github.com/alikaragoz/MCSwipeTableViewCell 我想知道使用此库是否与我有任何关系github.com/alikaragoz/MCSwipeTableViewCell

It could also be because I'm using Firebase? 也可能是因为我正在使用Firebase?

First of all set delegates of gesture and then use this delegate, it could help you : 首先设置手势的代表,然后使用此代表,它可以帮助您:

override func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return false;
}

I eventually did this, using the callbacks made available by the table cell pod i'm using: 我最终使用了我正在使用的表格单元格窗格提供的回调来做到这一点:

class OrderTableManager {
  static var swiping = false
}

class OrderPreparingTableViewCell: MCSwipeTableViewCell, MCSwipeTableViewCellDelegate {
  override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
    if !OrderTableManager.swiping {
      OrderTableManager.swiping = true
      return true
    } else {
      return false
    }
  }

  // MARK: Public
  func swipeTableViewCellDidEndSwiping(_ cell: MCSwipeTableViewCell!) {
    OrderTableManager.swiping = false
  }

  func displayOrder(order: AppState.Order, clock: Clock, fDone: @escaping SwipeHandler, fDelete: @escaping SwipeHandler) -> OrderPreparingTableViewCell {
    ...

    self.delegate = self

    ...
  }

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

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