研究了有一天了秉颗,剛找到方法去實現(xiàn)自定義圓角左滑cell的刪除按鈕吩蔑,主要是一步一步打印出UITableView的視圖層級(Xcode 9.0 iOS 10+)
在UITableView的ViewController的- (void)viewDidLayoutSubviews實現(xiàn)应民。
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];
同時要實現(xiàn)代理
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
? ? self.editingIndexPath = indexPath;
? ? [self.view setNeedsLayout];? // 觸發(fā)-(void)viewDidLayoutSubviews
}
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
? ? self.editingIndexPath = nil;
}
[self.view setNeedsLayout];必須要實現(xiàn)
以下是全部代碼
- (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];? // 觸發(fā)-(void)viewDidLayoutSubviews
}
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
? ? self.editingIndexPath = nil;
}
- (void)configDeleteButton:(UIButton*)deleteButton
{
? ? if (deleteButton)
? ? {
? //點擊刪除按鈕觸發(fā)的方法
? ? }
}
- (void)configReadButton:(UIButton*)readButton
{
? ? if (readButton)
? ? {
? ? }
}
#pragma mark - 返回編輯模式,默認(rèn)為刪除模式
-(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{//請求數(shù)據(jù)源提交的插入或刪除指定行接收者卵佛。
? ? 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