2017-10-31 3 views
0

私はどのボタンが押されたかに基づいてセルを変更するテーブルビューを持っています。最初のテーブルビューでは、ボタンが押された場合にのみセルをスワイプできます。そうでなければスワイプ機能を無効にする必要があります。ボタンが押されると、tableviewはスワイプ可能で、必要な動作を表示します。しかし、ボタンが押されていない場合、セルはまだ「削除」アクションでスワイプ可能です。また、セルのラベルに現在のユーザーの名前が含まれている場合は、行をスワイプ可能にする必要はありません。特定のセルでのみスワイプする

テーブルビューのコードを一番下に追加しました。これをどうやって解決するのですか?第一テーブルの

コード:

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 
       let cell = tableView.cellForRow(at: indexPath) as! DetailsCell 
       let detailsAction = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: "Details") { (action , indexPath) -> Void in 
         if isCharacterButtonPressed == true { //if button is pressed 
          if cell.label != currentUser.name{ 
           let viewController = self.storyboard?.instantiateViewController(withIdentifier: "SeeDetails") as! DetailsVC 
           viewController.person = cell.person    
     self.navigationController?.pushViewController(viewController, animated: true) 
          } else { 
           return .none 
          } 
          return [detailsAction] //makes cell swipe-able 
         } else { //if button is not pressed 
          return .none //do not make the cell swipe-able 
         } 
       } 

EDIT:

私はcanEditRow機能とそれを固定しているが、これらの赤い丸がIスワイプすることができ、細胞の側に表示スワイプします。これをどうやって解決するのですか?あなたがアクションの空の配列を返すとき、任意の編集アクションを表示しないようにするので、ユーザーエクスペリエンスがずさんになり、tableView(_: UITableView, editActionsForRowAt: IndexPath)で編集アクションを表示するかどうかをチェックする場合は、セルを enter image description here

答えて

1

:私はそれのスクリーンショットを添付していますあたかも一度スワイプされたように見えてから、スワイプに反応しなくなります。代わりに、tableview(_:UITableView,canEditRowAt:IndexPath)を使用してセルの編集アクションを表示するかどうかを確認してから、セルがスワイプ可能なときに他の機能を実際に表示する必要があります。

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 
    let cell = tableView.cellForRow(at: indexPath) as! DetailsCell 
    if isCharacterButtonPressed == true && cell.label != currentUser.name{ 
     return true 
    } else { 
     return false 
    } 
} 
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 
    let cell = tableView.cellForRow(at: indexPath) as! DetailsCell 
    let detailsAction = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: "Details") { (action , indexPath) -> Void in 
    let viewController = self.storyboard?.instantiateViewController(withIdentifier: "SeeDetails") as! DetailsVC 
    viewController.person = cell.person    
    self.navigationController?.pushViewController(viewController, animated: true) 
    }   
    return [detailsAction]   
} 
0

使用のいずれかtableView(_: UITableView, editActionsForRowAt: IndexPath)またはMGSwipTableCell、ブール値(すなわちallowSwipe)、のみ編集/スワイプBOOLがtrueの場合は許可を作成

関連する問題