转载: https://www.jianshu.com/p/779f36c21632
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(100, 66, 480, 640) style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
[self.view addSubview:self.tableView];
self.dataArray = [NSMutableArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f", nil];
}
- (void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
if (self.editingIndexPath) {
[self configSwipeButtons];
}
}
- (void)configSwipeButtons
{
// 获取选项按钮的reference
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"11.0"))
{
// iOS 11层级 (Xcode 9编译): UITableView -> UISwipeActionPullView
for (UIView *subview in self.tableView.subviews)
{
if ([subview isKindOfClass:NSClassFromString(@"UISwipeActionPullView")] && [subview.subviews count] >= 2)
{
// 和iOS 10的按钮顺序相反
UIButton *deleteButton = subview.subviews[1];
UIButton *readButton = subview.subviews[0];
[self configDeleteButton:deleteButton];
[self configReadButton:readButton];
}
}
}
else
{
// iOS 8-10层级: UITableView -> UITableViewCell -> UITableViewCellDeleteConfirmationView
UITableViewCell *tableCell = [self.tableView cellForRowAtIndexPath:self.editingIndexPath];
for (UIView *subview in tableCell.subviews)
{
if ([subview isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")] && [subview.subviews count] >= 2)
{
UIButton *deleteButton = subview.subviews[0];
UIButton *readButton = subview.subviews[1];
[self configDeleteButton:deleteButton];
[self configReadButton:readButton];
// [subview setBackgroundColor:[UIColor redColor]];
}
}
}
}
- (void)configDeleteButton:(UIButton*)deleteButton
{
if (deleteButton)
{
[deleteButton.titleLabel setFont:[UIFont fontWithName:@"SFUIText-Regular" size:12.0]];
[deleteButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[deleteButton setImage:[UIImage imageNamed:@"Delete_icon_.png"] forState:UIControlStateNormal];
[deleteButton setBackgroundColor:[UIColor whiteColor]];
// 调整按钮上图片和文字的相对位置(该方法的实现在下面)
[self centerImageAndTextOnButton:deleteButton];
}
}
- (void)configReadButton:(UIButton*)readButton
{
if (readButton)
{
[readButton.titleLabel setFont:[UIFont fontWithName:@"SFUIText-Regular" size:12.0]];
[readButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
// 根据当前状态选择不同图片
UIImage *readButtonImage = [UIImage imageNamed: @"Mark_as_read_icon_.png"];
[readButton setImage:readButtonImage forState:UIControlStateNormal];
[readButton setBackgroundColor:[UIColor whiteColor]];
// 调整按钮上图片和文字的相对位置(该方法的实现在下面)
[self centerImageAndTextOnButton:readButton];
}
}
- (void)centerImageAndTextOnButton:(UIButton*)button
{
// this is to center the image and text on button.
// the space between the image and text
CGFloat spacing = 35.0;
// lower the text and push it left so it appears centered below the image
CGSize imageSize = button.imageView.image.size;
button.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, - (imageSize.height + spacing), 0.0);
// raise the image and push it right so it appears centered above the text
CGSize titleSize = [button.titleLabel.text sizeWithAttributes:@{NSFontAttributeName: button.titleLabel.font}];
button.imageEdgeInsets = UIEdgeInsetsMake(-(titleSize.height + spacing), 0.0, 0.0, - titleSize.width);
// increase the content height to avoid clipping
CGFloat edgeOffset = (titleSize.height - imageSize.height) / 2.0;
button.contentEdgeInsets = UIEdgeInsetsMake(edgeOffset, 0.0, edgeOffset, 0.0);
// move whole button down, apple placed the button too high in iOS 10
if (SYSTEM_VERSION_LESS_THAN(@"11.0"))
{
CGRect btnFrame = button.frame;
btnFrame.origin.y = 18;
button.frame = btnFrame;
}
}
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
self.editingIndexPath = indexPath;
[self.view setNeedsLayout]; // 触发-(void)viewDidLayoutSubviews
}
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
self.editingIndexPath = nil;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 120;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellID = @"tableview.cell.id";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
}
cell.textLabel.text = [NSString stringWithFormat:@"第%d cell",indexPath.row];
return cell;
}
- (NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
// delete action
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:NSLocalizedString(@"Delete", @"") handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
[tableView setEditing:NO animated:YES]; // 这句很重要,退出编辑模式,隐藏左滑菜单
[self removeNotificationAction:indexPath];
}];
// read action
// 根据cell当前的状态改变选项文字
NSString *readTitle = @"Read";
// 创建action
UITableViewRowAction *readAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:readTitle handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
[tableView setEditing:NO animated:YES]; // 这句很重要,退出编辑模式,隐藏左滑菜单
}];
return @[deleteAction, readAction];
}
- (void)removeNotificationAction:(NSIndexPath *)indexpath
{
[self.dataArray removeObjectAtIndex:indexpath.row];
[self.tableView reloadData];
}