咱们都知道 UITableView
支持实现侧滑操做,通常用来实现删除一个项目,实现起来也很简单,只须要实现 UITableView
的三个代理方法swift
UITableView
咱们须要实现的操做类型,好比返回一个.delete
public func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return .delete
}
复制代码
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
return "Delete"
}
复制代码
public func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
// Do somthing
}
复制代码
几个项目下来,咱们对这个三部曲早就是倒背如流了。不过有时候会想,一个简单的操做按钮,须要实现三个 Delegate 方法来达到效果,会不会太繁琐了。。。api
更况且还有一件更坑的事情:editingStyleForRow
和titleForDeleteConfirmationButton
这两个方法属于UITableViewDelegate
协议,而commit editingStyle
这个方法属于UITableViewDataSource
协议。数组
这意味着,若是你为UITableView
实现了通用的DataSource
协议,那么要实现侧滑操做就不可避免要破坏代码结构了。。。闭包
另外,侧滑支持多项操做的需求愈来愈旺盛,而此时咱们的经典三部曲已经没法胜任工做了。spa
这时候要么选择第三方的实现方案,作好随时被坑的准备;要么,本身去实现一个更大的。。。坑?设计
估计苹果的工程师也为本身这个天才的设计折服了吧,因而在 iOS 8 引入了一个新的 API:UITableViewRowAction
,先来看一看定义压压惊:代理
@available(iOS 8.0, *)
open class UITableViewRowAction : NSObject, NSCopying {
public convenience init(style: UITableViewRowActionStyle, title: String?, handler: @escaping (UITableViewRowAction, IndexPath) -> Swift.Void)
open var style: UITableViewRowActionStyle { get }
open var title: String?
@NSCopying open var backgroundColor: UIColor? // default background color is dependent on style
@NSCopying open var backgroundEffect: UIVisualEffect?
}
复制代码
先来看构造器,便利构造器接受三个属性:style
、title
、handler
。code
title
这个不用说了,确定就是按钮显示的标题了。orm
style
经过定义能够看到,是一个UITableViewRowActionStyle
类型的枚举值,经过构造器传入以后便不可更改了,想来是决定操做按钮的显示样式的吧,destructive
这个单词是否是很熟悉?事件
@available(iOS 8.0, *)
public enum UITableViewRowActionStyle : Int {
case `default`
case destructive
case normal
}
复制代码
继续往下看到handler
,这是一个闭包,显然是用来响应按钮点击事件的了,终于不用另外实现一个Delegate
方法去响应操做事件了么?自从 iOS 4 以后引入了 block,苹果已经一发不可收拾了,API 改造大军正在路上。。。
class 定义里面还有两个属性:
首先看到backgroundColor
,字面意思很好理解,就是背景颜色了,后面注释了一行小字:default background color is dependent on style。
这证明了咱们的猜测:style
属性决定了按钮的样式,也就是背景颜色,固然,咱们也能够经过backgroundColor
本身另外指定背景色。
最后一个属性是backgroundEffect
,是UIVisualEffect
类型的。UIVisualEffect
?!这是啥?有经验的同窗都知道,这是 iOS 7 以后引入的毛玻璃特效啊,不过研究了半天发现并无卵用😂。也多是我打开的方式不对?有知道的同窗能够指点下。。。
###实践 好了,看完了类定义,赶忙来看看怎么使用吧。先看一下UITableViewDelegate
的定义,关于UITableViewRowAction
只定义了一个方法:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
复制代码
终于决定抛弃三部曲了么😳,返回值是个数组,这是支持多个操做项的节奏啊,废话少说上代码:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let defaultAction = UITableViewRowAction(style: .default, title: "Default") { (action, indexPath) in
self.alertTitle("Default action at \(indexPath)")
}
let normalAction = UITableViewRowAction(style: .normal, title: "Normal") { (action, indexPath) in
self.alertTitle("Normal action at \(indexPath)")
}
let destructiveAction = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
self.alertTitle("Delete action at \(indexPath)")
}
return [defaultAction, normalAction, destructiveAction]
}
func alertTitle(_ title: String) {
let alert = UIAlertController(title: title, message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
present(alert, animated: true)
}
复制代码
这里定义了三个UITableViewRowAction
,分别对应不一样的style
,运行以后能够发现,default
和destructive
默认都是红色,normal
默认是灰色。这里只是简单定义了一个alertTitle
方法用来响应点击反馈,实际项目中须要替换成特定的业务逻辑。
###结语 至此终于水完了UITableViewRowAction
相关的内容,虽然这是个 iOS 8 就出来的特性了,不过最近才被我注意到,并且尚未把backgroundEffect
属性的特性摸清,实在是惭愧。
好消息是如今大多数 App 都是至少支持 iOS 8 + 了吧?能够不再用写繁琐的三部曲了。