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で右スワイプだけではなく左スワイプにも対応する