LoginSignup
19
17

More than 3 years have passed since last update.

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

Last updated at Posted at 2016-04-03

TableViewでスワイプしたらメニューボタンを出す機能を実装する方法。

環境

環境 バージョン
Xcode 7.3
Swift 2.2

参照

UITableViewDelegate
UITableViewRowAction

実装

    // Buttonを拡張する.
    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
        // Shareボタン.
        let myShareButton: UITableViewRowAction = UITableViewRowAction(style: .Normal, title: "Share") { (action, index) -> Void in

            tableView.editing = false
            print("share")

        }
        myShareButton.backgroundColor = UIColor.blueColor()

        // Archiveボタン.
        let myArchiveButton: UITableViewRowAction = UITableViewRowAction(style: .Normal, title: "Archive") { (action, index) -> Void in

            tableView.editing = false
            print("archive")

        }
        myArchiveButton.backgroundColor = UIColor.grayColor()

        // Deleteボタン.
        let myDeleteButton: UITableViewRowAction = UITableViewRowAction(style: .Normal, title: "Delete") { (action, index) -> Void in

            tableView.editing = false
            print("delete")

        }
        myDeleteButton.backgroundColor = UIColor.redColor()

        return [myShareButton, myArchiveButton, myDeleteButton]

    }

ぼやき

逆スワイプでボタンを表示したい場合はどうするんだろうか・・・

swiftじゃないけど、こういうの:SWTableViewCellを使えばいいのかなー?

※追加@2016/4/5※
SWTableViewCellを使って逆スワイプにも対応する方法を書きました。
SWTableViewCellを使ってTableViewで右スワイプだけではなく左スワイプにも対応する

19
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
17