研究了有一天了,刚找到方法去实现自定义圆角左滑cell的删除按钮,主要是一步一步打印出UITableView的视图层级(Xcode 9.0 iOS 10+)
在UITableView的ViewController的- (void)viewDidLayoutSubviews实现。
iOS 11 (Xcode 9编译): UITableView -> UISwipeActionPullView -> UISwipeActionStandardButton
滑动展示的Delete按钮在_tableVeiw.subviews上,打印出_tableVeiw.subviews,里面有一个UISwipeActionStandardButton,取出这个按钮就是删除按钮
UIButton *deleteButton = subview.subviews[0];
deleteButton.frame = CGRectMake(0, 5, 70, 110);
deleteButton.backgroundColor = [UIColor whiteColor];
[deleteButton setBackgroundImage:[UIImage imageNamed:@"Group 5"] forState:UIControlStateNormal];
同时要实现代理
- (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;
}
[self.view setNeedsLayout];必须要实现
以下是全部代码
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
if (self.editingIndexPath)
{
[self configSwipeButtons];
}
}
- (void)configSwipeButtons
{
// 获取选项按钮的reference
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"11.0")){
for (UIView *subview in _tableVeiw.subviews)
{
subview.backgroundColor = [UIColor whiteColor];
if ([subview isKindOfClass:NSClassFromString(@"UISwipeActionPullView")])
{
NSLog(@"上面的视图 %@",subview.subviews);
// 和iOS 10的按钮顺序相反
UIButton *deleteButton = subview.subviews[0];
deleteButton.frame = CGRectMake(0, 5, 70, 110);
// deleteButton.layer.cornerRadius = 10;
// deleteButton.layer.masksToBounds = YES;
deleteButton.backgroundColor = [UIColor whiteColor];
[deleteButton setBackgroundImage:[UIImage imageNamed:@"Group 5"] forState:UIControlStateNormal];
[self configDeleteButton:deleteButton];
[self centerImageAndTextOnButton:deleteButton];
}
}
}else{
// iOS 8-10层级: UITableView -> UITableViewCell -> UITableViewCellDeleteConfirmationView
NoticeTableVC *tableCell = [_tableVeiw cellForRowAtIndexPath:self.editingIndexPath];
for (UIView *subview in tableCell.subviews)
{
subview.backgroundColor = [UIColor whiteColor];
if ([subview isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")])
{
UIButton *deleteButton = subview.subviews[0];
[self configDeleteButton:deleteButton];
deleteButton.frame = CGRectMake(0, 5, 60, 110);
// deleteButton.layer.cornerRadius = 10;
// deleteButton.layer.masksToBounds = YES;
deleteButton.backgroundColor = [UIColor whiteColor];
[deleteButton setBackgroundImage:[UIImage imageNamed:@"Group 5"] forState:UIControlStateNormal];
[self configDeleteButton:deleteButton];
[self centerImageAndTextOnButton:deleteButton];
}
}
}
}
- (void)centerImageAndTextOnButton:(UIButton*)button
{
if (SYSTEM_VERSION_LESS_THAN(@"11.0"))
{
CGRect btnFrame = button.frame;
btnFrame.origin.y = 30;
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;
}
- (void)configDeleteButton:(UIButton*)deleteButton
{
if (deleteButton)
{
//点击删除按钮触发的方法
}
}
- (void)configReadButton:(UIButton*)readButton
{
if (readButton)
{
}
}
#pragma mark - 返回编辑模式,默认为删除模式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"删除";
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{//请求数据源提交的插入或删除指定行接收者。
if (editingStyle == UITableViewCellEditingStyleDelete) {//如果编辑样式为删除样式
}else {
}
}
-(void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Tapped accessory button");
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
}
项目demo地址:https://gitee.com/1023678795/LeftSliderDemo.git