实现 UITableViewCell 侧滑操做列表

曾经

咱们都知道 UITableView 支持实现侧滑操做,通常用来实现删除一个项目,实现起来也很简单,只须要实现 UITableView 的三个代理方法swift

  • 首先告诉UITableView咱们须要实现的操做类型,好比返回一个.delete
public func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
	return .delete
}
复制代码
  • 而后告诉 UITableView 侧滑时删除按钮上显示的文字
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

更况且还有一件更坑的事情:editingStyleForRowtitleForDeleteConfirmationButton这两个方法属于UITableViewDelegate协议,而commit editingStyle这个方法属于UITableViewDataSource协议。数组

这意味着,若是你为UITableView实现了通用的DataSource协议,那么要实现侧滑操做就不可避免要破坏代码结构了。。。闭包

另外,侧滑支持多项操做的需求愈来愈旺盛,而此时咱们的经典三部曲已经没法胜任工做了。spa

这时候要么选择第三方的实现方案,作好随时被坑的准备;要么,本身去实现一个更大的。。。坑?设计

iOS 8 以后

估计苹果的工程师也为本身这个天才的设计折服了吧,因而在 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?
}
复制代码

先来看构造器,便利构造器接受三个属性:styletitlehandlercode

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,运行以后能够发现,defaultdestructive默认都是红色,normal默认是灰色。这里只是简单定义了一个alertTitle方法用来响应点击反馈,实际项目中须要替换成特定的业务逻辑。

###结语 至此终于水完了UITableViewRowAction相关的内容,虽然这是个 iOS 8 就出来的特性了,不过最近才被我注意到,并且尚未把backgroundEffect属性的特性摸清,实在是惭愧。

好消息是如今大多数 App 都是至少支持 iOS 8 + 了吧?能够不再用写繁琐的三部曲了。

相关文章
相关标签/搜索