在項(xiàng)目需求中捅膘,經(jīng)常會(huì)有對(duì) TableView Cell 左滑事件及按鈕的自定義設(shè)置需求
iOS 11.0之前
左滑按鈕
在iOS 11.0之前,我們可以看到 Cell 的左滑界面UITableViewCellDeleteConfirmationView
布局是在于 Cell 之中的,并且只有左滑事件之后才會(huì)添加到 Cell 中遣蚀,在左滑界面中存在一個(gè)UIButton
的子視圖扳碍,這便是我們需要自定義的視圖
在自定義的 TableViewCell 中,重寫(xiě)
- (void)didTransitionToState:(UITableViewCellStateMask)state
- (void)didTransitionToState:(UITableViewCellStateMask)state {
[super didTransitionToState:state];
if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask) {
NSLog(@"%@", self.subviews);
for (UIView *subview in self.subviews) {
if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) {
subview.backgroundColor = [UIColor clearColor];
UIButton *btn = [subview.subviews objectAtIndex:0];
[btn setImage:[UIImage imageNamed:@"icon_delete"] forState:UIControlStateNormal];
[btn setTitle:nil forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor clearColor]];
}
}
}
}
左滑事件
在 tableView 的 delegate 方法中自帶有 - (NSArray *)tableView:(UITableView*)tableView editActionsForRowAtIndexPath:(NSIndexPath*)indexPath
可自定義事件
- (UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath*)indexPath {
UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:nil handler:^(UITableViewRowAction *_Nonnullaction, NSIndexPath *_NonnullindexPath) {
[self showAlertWithIndexPath:indexPath];
}];
rowAction.backgroundColor = [UIColor cyanColor];
return @[rowAction];
}
至此iOS 11.0之前的版本的左滑自定義便已經(jīng)可以了(當(dāng)然宰僧,也可以自定義左滑時(shí)多個(gè)事件,原理也是一樣的)
iOS 11.0之后
左滑按鈕
在iOS 11.0之后的版本观挎,左滑布局發(fā)生了改變
我們可以看到琴儿,此時(shí)也已經(jīng)不是
UITableViewCellDeleteConfirmationView
,而是UISwipeActionPullView
嘁捷,并且是UITableView
的子視圖造成,因此,在 Cell 中重寫(xiě)- (void)didTransitionToState:(UITableViewCellStateMask)state
在iOS 11.0之后的系統(tǒng)并無(wú)作用雄嚣。這是可以使用tableView的代理方法
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
if (@available(iOS 11.0, *)) {
[self customDeleteBtnAfteriOS11:tableView];
}
}
//在此方法中并不能有效的設(shè)置BGColor為clearColor
- (void)customDeleteBtnAfteriOS11:(UITableView *)tableView {
for (UIView *subview in tableView.subviews) {
if ([NSStringFromClass([subview class]) isEqualToString:@"UISwipeActionPullView"]) {
// subview.backgroundColor = [UIColor clearColor];
UIButton *btn = [subview.subviews objectAtIndex:0];
[btn setImage:[UIImage imageNamed:@"icon_delete"] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
// [btn setBackgroundColor:[UIColor clearColor]];
}
}
}
左滑事件
iOS 11.0之后的當(dāng)然能用之前的方法了晒屎,而蘋(píng)果在11.0之后其實(shí)也新增了另外一個(gè)新的方法- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
,只在iOS 11.0之后的系統(tǒng)才有效缓升,其實(shí)目前區(qū)別上也僅僅是action多了一個(gè)image屬性鼓鲁,然而此image屬性也不能顯示原圖,而是會(huì)被自動(dòng)渲染港谊。
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
if (@available(iOS 11.0, *)) {
WeakObj(self);
UIContextualAction *action = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal
title:@"刪除按鈕"
handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
StrongObj(self);
[selfStrong showAlertWithIndexPath:indexPath];
completionHandler(YES);
}];
//action.image = [UIImage imageNamed:@"icon_delete"];//若僅設(shè)置此屬性骇吭,會(huì)被系統(tǒng)渲染
action.backgroundColor = [UIColor cyanColor];
return [UISwipeActionsConfiguration configurationWithActions:@[action]];
}
return nil;
}