- (void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
[self configSwipeButtons];
}
-(void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath*)indexPath{
self.indexPath = indexPath;
[self.view setNeedsLayout];
}
- (void)configSwipeButtons{
// iOS 11层级 : UITableView -> UISwipeActionPullView
if (@available(iOS 11.0, *)) {
for (UIView *subview in self.tableView.subviews)
{
//NSLog(@"%@",NSStringFromClass([subview class]));
//这里写大于等于2是因为我这里需要两个action
if ([subview isKindOfClass:NSClassFromString(@"UISwipeActionPullView")] && [subview.subviews count] >= 2)
{
// 和iOS 10的按钮顺序相反
UIButton *deleteButton = subview.subviews[1];
[deleteButton setImage:[UIImage imageNamed:@"photo_delete"] forState:UIControlStateNormal];
UIButton *readButton = subview.subviews[0];
[readButton setImage:[UIImage imageNamed:@"test_normal"] forState:UIControlStateNormal];
}
}
}else{
// iOS 8-10层级: UITableView -> UITableViewCell -> UITableViewCellDeleteConfirmationView
WTSendMessageCell *tableCell = [self.tableView cellForRowAtIndexPath:self.indexPath];
for(UIView *subview in tableCell.subviews){
if ([subview isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")] && [subview.subviews count] >= 2){
UIButton*deleteButton = subview.subviews[1];
[deleteButton setImage:[UIImage imageNamed:@"photo_delete"] forState:UIControlStateNormal];
UIButton *readButton = subview.subviews[0];
[readButton setImage:[UIImage imageNamed:@"test_normal"] forState:UIControlStateNormal];
}
}
}
}
#pragma mark - UITableView滑动删除
// 先要设Cell可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{ // 添加一个删除按钮
UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"点击了删除");
}];
// deleteRowAction.backgroundColor = [UIColor blackColor];
// 删除一个置顶按钮
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];
}