UITableViewRowAction对象是一个用户水平左滑UITableViewCell时显示的单独的按钮。默认情况下,左滑会显示一个删除按钮。这个类会帮助实现一个或这多个按钮。
自定义的按钮需要添加在tableView:editActionsForRowAtIndexPath:代理方法中.
初始化:
<pre name="code" class="objc"> //在此方法中实现
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
//style:Default默认红色,Normal默认灰色
//title时按钮的文字
//handler是点击时候的回调函数,相当于block
let deleteAction = UITableViewRowAction(style: .Default, title: "Delete", handler: { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in
println("Delete button of row\(indexPath.row) have been Tapped")
})
let moreAction = UITableViewRowAction(style: .Normal, title: "More", handler: { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in
println("More button of row\(indexPath.row) have been Tapped")
})
//数组前面的放在后面显示
return [deleteAction, moreAction]
}
//即使没有实现也需要重写这个方法
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
}
自定义样式:修改颜色和模糊效果
<span style="font-family:Times New Roman;"><span style="font-size:14px;">deleteAction.backgroundColor = UIColor.blueColor()
deleteAction.backgroundEffect = UIBlurEffect(style:UIBlurEffectStyle.Dark)
moreAction.backgroundColor = UIColor.greenColor()
moreAction.backgroundEffect = UIBlurEffect(style:UIBlurEffectStyle.Dark)</span></span>