昨天找了一天的问题,为了定制一个tableViewCell删除按钮。下面直接把代码贴上吧,其中有部分来自网络搜寻的结果,但原链接已经找不到了。
#pragma mark -- UITableViewDelegate, UITableViewDataSource
- (BOOL)tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath {
return YES;
}
- (NSArray *)tableView:(UITableView*)tableView editActionsForRowAtIndexPath:(NSIndexPath*)indexPath {
// 添加一个删除按钮
UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Remove" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"点击了删除");
}];
// ios 10 以下有用
deleteRowAction.backgroundColor = UIColor.clearColor;
// 将设置好的按钮放到数组中返回
return@[deleteRowAction];
/* **** 可以添加更多的按钮 ***
// 添加一个置顶按钮
UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"点击了置顶");
}];
topRowAction.backgroundColor = [UIColor blueColor];
// 添加一个更多按钮
UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"点击了更多"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"点击了更多");
}];
moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
// 将设置好的按钮放到数组中返回
return @[deleteRowAction, topRowAction, moreRowAction];*/
}
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath*)indexPath{
self.indexPath= indexPath;
[self.view setNeedsLayout]; // 这里必须要加上
}
- (void)configSwipeButtons {
// iOS 11层级 : UITableView -> UISwipeActionPullView
if(@available(iOS11.0, *)) { // ios 11 以及以上版本走这里
for(UIView*subviewinself.speakerTableView.subviews) {
if([subviewisKindOfClass:NSClassFromString(@"UISwipeActionPullView")]) {
NSLog(@"subview.subviews = %@", subview.subviews[0].subviews);
if([subview.subviewscount] ==1) {
UIButton*deleteButton = subview.subviews[0];
// 按钮添加图片 和文字,这里添加之后,自动布局为上面图片,下面文字
[deleteButtonsetImage:[UIImage imageNamed:@"email"] forState:UIControlStateNormal];
[deleteButtonsetTitle:@""forState:UIControlStateNormal];
[deleteButtonsetTitle:@"" forState:UIControlStateHighlighted];
// 按钮的背景颜色可以随便改,但是要改为透明,需要如下操作:
UIView*delView = subview.subviews[0].subviews[0];
delView.backgroundColor=UIColor.redColor;
}
/* *******添加更多按钮******
else if ([subview.subviews count] >= 2) {
// 这里写大于等于2是因为这里可以两个action
// 和iOS 10的按钮顺序相反,
UIButton *deleteButton2 = subview.subviews[2];
[deleteButton2 setImage:[UIImage imageNamed:@"email"] forState:UIControlStateNormal];
UIButton *deleteButton = subview.subviews[1];
[deleteButton setImage:[UIImage imageNamed:@"email"] forState:UIControlStateNormal];
UIButton *readButton = subview.subviews[0];
[readButton setImage:[UIImage imageNamed:@"Phone"] forState:UIControlStateNormal];
}*/
}
}
}else{ // ios 10 以及以下版本走这里。
// iOS 8-10层级: UITableView -> UITableViewCell -> UITableViewCellDeleteConfirmationView
HNGSpeakerTVCell *tableCell = [self.speakerTableView cellForRowAtIndexPath:self.indexPath];
for(UIView*subviewintableCell.subviews) {
if ([subview isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
if([subview.subviewscount] ==1) {
UIButton*deleteButton = subview.subviews[0];
[deleteButtonsetImage:[UIImage imageNamed:@"email"] forState:UIControlStateNormal];
// 设置文字为空
[deleteButtonsetTitle:@""forState:UIControlStateNormal];
[deleteButtonsetTitle:@"" forState:UIControlStateHighlighted];
}
/*else if ([subview.subviews count] >= 2) {
UIButton *deleteButton2 = subview.subviews[1];
[deleteButton2 setImage:[UIImage imageNamed:@"email"] forState:UIControlStateNormal];
UIButton*deleteButton = subview.subviews[1];
[deleteButton setImage:[UIImage imageNamed:@"email"] forState:UIControlStateNormal];
UIButton *readButton = subview.subviews[0];
[readButton setImage:[UIImage imageNamed:@"Phone"] forState:UIControlStateNormal];*/
}
}
}
}
最后说明一点:在 ios11 以下的版本,文字和图片 需要调整位置
(UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)]),
最简单的办法就是,把文字清空,让美工小姐姐,给一个 图文并存的图片。