在我们的应用开发当中,经常会用到UITableView的左滑删除的功能,通常的话效果如下
但有时候系统现有的功能并不能完全满足我们的开发需求,这样就需要我们在其现有的功能基础上自定义我们所需要的功能了。下图是在项目中自定义的按钮(只是修改了按钮的框架而已)。
然后我就总结了一下根据不同的需求自定义不同的按钮。
如果你对左滑删除按钮的要求不高,仅仅只是实现UITableView上细胞的左滑删除功能,那在UITableView的代理方法中添加以下两种方法便可实现需求:
//使用系统默认的删除按钮 - (void) tableView: (UITableView *) tableView commitEditingStyle: (UITableViewCellEditingStyle) editingStyle forRowAtIndexPath: (NSIndexPath *) indexPath { 如果(editingStyle==UITableViewCellEditingStyleDelete) { } }//自定义系统默认的删除按钮文字 ——(NSString *) tableView: (UITableView *) tableView titleForDeleteConfirmationButtonForRowAtIndexPath: (NSIndexPath *) indexPath { 回复@“自定义按钮”; }
效果如下所示:
//自定义多个左滑菜单选项
——(NSArray 在上述代理方法中我们就可以实现在细胞中添加一个或多个左滑按钮了,根据点击不同的按钮实现不同的响应方法便可。其中<代码> (tableView setEditing:没有动画:是的),代码>方法可以在点击按钮之后退出编辑模式并隐藏左滑菜单。但如果我们想要修改按钮的其他属性如标题,背景颜色怎么办?点击进入UITableViewRowAction类中,我们会发现以下属性和方法:
@ interface UITableViewRowAction: NSObject & lt; NSCopying>
+ (instancetype) rowActionWithStyle:(UITableViewRowActionStyle)风格的标题:标题(nullable NSString *)处理程序:(空白(^)(UITableViewRowAction *行动,NSIndexPath * indexPath))处理程序;
@ property(原子只读)UITableViewRowActionStyle风格;
@ property(原子、复制nullable) NSString *标题;
@ property(原子、复制nullable)用户界面颜色*写成backgroundColor;//默认背景颜色是依赖>
typedef NS_ENUM (NSInteger UITableViewRowActionStyle) {
UITableViewRowActionStyleDefault=0,//红底白字
UITableViewRowActionStyleDestructive=UITableViewRowActionStyleDefault,
UITableViewRowActionStyleNormal//灰底白字
}NS_ENUM_AVAILABLE_IOS (8 _0) __TVOS_PROHIBITED;
iOS自定义UITableView实现不同系统下的左滑删除功能详解