TableViewをスワイプしたらボタンを表示させる

削除以外のアクションを定義したく調べたものの、メモです

UITableViewDelegateを適用した上で、以下でOKです。

@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

  let delete = UIContextualAction(style: .destructive, title: "削除") { (action, sourceView, completionHandler) in completionHandler(true)
    print("削除")
    tableView.deleteRows(at: [indexPath], with: .fade)
  }

  let share = UIContextualAction(style: .normal, title: "保存") { (action, sourceView, completionHandler) in completionHandler(true)         
    print("保存")
  }

  let swipeAction = UISwipeActionsConfiguration(actions: [delete, share])
  swipeAction.performsFirstActionWithFullSwipe = false
  return swipeAction
}
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.