在iOS7系统的Mail App中TableViewCell的一个功能让咱们作TableView的比较羡慕,就是滑动cell,右边出现了两个按钮,以下:git
网上在github上有不少大牛用custom tableViewCell的方法实现了这个效果,甚至更强,好比这两位:github
https://github.com/CEWendel/SWTableViewCellapp
https://github.com/daria-kopaliani/DAContextMenuTableViewControllerspa
可是自定义实现的确比较麻烦,最终终于找到了apple官方支持这个的方法,也是github上的一个大牛,用OC方式实现了:3d
https://gist.github.com/marksands/76558707f583dbb8f870code
调用了apple官方的API:blog
func tableView(tableView: UITableView!, editActionsForRowAtIndexPath indexPath: NSIndexPath!) -> [AnyObject]!
大牛都用OC实现了,我再复制过来也没意思,因此我用Swift语言实现下,代码以下:it
func tableView(tableView: UITableView!, editActionsForRowAtIndexPath indexPath: NSIndexPath!) -> [AnyObject]! { var moreAction: UITableViewRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "More", handler:{(tableViewRowAction:UITableViewRowAction!, index:NSIndexPath!) -> Void in println("More tap") }) moreAction.backgroundColor = UIColor.lightGrayColor() var deleteAction: UITableViewRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete", handler: {(tableViewRowAction:UITableViewRowAction!, index:NSIndexPath!) -> Void in println("Delete tap") }) deleteAction.backgroundColor = UIColor.redColor() return [deleteAction, moreAction] }
这个代码在Xcode6-Beta5下正常运行,而且成功输出"More tap" & "Delete tap"io
剩下的代码,能够本身补全,剩下的个经常使用的操做方式同样,或者能够参照github上大牛的代码。table
如今只是向左滑动有按钮,若是向右滑动也有按钮就行了,若是能够简单实现,我在写上来。