改变UITableViewCell中的UITableViewCellDeleteConfirmationControl(删除button)的颜色

目前我正在尝试自定义UITableViewCell中的deltebutton。 现在我有这样的事情:

在这里输入图像说明

现在,所有我需要的是改变这个button的颜色,我不想改变行为,或使其绝对自定义。 我敢肯定,这是可能的,而不需要创build自己的控件来删除UITableView中的行。

这我的代码:

- (void)layoutSubviews { [super layoutSubviews]; for (UIView *subview in self.subviews) { if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) { UIView *deleteButtonView = (UIView *)[subview.subviews objectAtIndex:0]; deleteButtonView.backgroundColor = [UIColor greenColor]; } } } 

我怎么能这样做?

提前致谢!

UITableViewCellDeleteConfirmationControl不是公共类,你不能轻易改变它的外观。

为了做到这一点,我相信你必须遍历单元格的子视图,并更改其属性:

 for (UIView *subview in self.subviews) { if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl") { // Set the background using an image. } } 

当然,由于它相当脆弱,所以你应该谨慎的做这样的事情。 我build议你自己动手。

我build议向苹果公司提交一个错误报告 ,请求更容易编辑的能力。

在UITableViewCell子类中:

 - (UIView*)recursivelyFindConfirmationButtonInView:(UIView*)view { for(UIView *subview in view.subviews) { if([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationButton"]) return subview; UIView *recursiveResult = [self recursivelyFindConfirmationButtonInView:subview]; if(recursiveResult) return recursiveResult; } return nil; } -(void)overrideConfirmationButtonColor { dispatch_async(dispatch_get_main_queue(), ^{ UIView *confirmationButton = [self recursivelyFindConfirmationButtonInView:self]; if(confirmationButton) confirmationButton.backgroundColor = [UIColor orangeColor]; }); } 

然后在UITableViewDelegate中:

 -(void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath { <#Your cell class#> *cell = (<#Your cell class#>*)[self.tableView cellForRowAtIndexPath:indexPath]; [cell overrideConfirmationButtonColor]; } 

这适用于iOS 7.1.2

以下是如何:

滑动时激活删除button

//确保在uitableviewcontroller中有以下方法

 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"You hit the delete button."); } 

设置自定义文本标签而不是删除。

 -(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath { return @"Your Label"; } 

设置button部分1的自定义颜色 – 警告,这在技术上涉及在私人苹果API戳。 但是,您不能阻止使用作为UIKIT一部分的公共方法search来修改子视图。

创build一个uitableviewcell类(另请参阅https://stackoverflow.com/a/22350817/1758337

 - (void)layoutSubviews { [super layoutSubviews]; for (UIView *subview in self.subviews) { //iterate through subviews until you find the right one... for(UIView *subview2 in subview.subviews){ if ([NSStringFromClass([subview2 class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) { //your color ((UIView*)[subview2.subviews firstObject]).backgroundColor=[UIColor blueColor]; } } } } 

另一个注意事项:这种方法不能保证将来的更新。 另外请注意,提及或使用私有UITableViewCellDeleteConfirmationView类可能会导致AppStore拒绝。

设置button部分2的自定义颜色

回到你的uitableviewcontroller

 - (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath { [YourTableView reloadData]; } 

(直到下一次在表格单元上调用layoutSubviews之前,才会调用替代颜色,所以我们通过重新加载所有东西来确保这种情况发生。)

silyevsk的例子非常好…但是,六个月之后,iOS 8已经出现,并且不再适当地工作。

现在,您需要查找名为“ _UITableViewCellActionButton ”的子视图并设置其背景颜色。

下面是我在我的应用程序中使用的silyevsk代码的修改版本。

在我的一些 UITableView单元格中,我不希望用户能够轻扫即可删除,但是我确实想要一些东西(一个挂锁图标)在他们滑动时出现。

在这里输入图像说明

为此,我在我的UITableViewCell类中添加了一个bReadOnlyvariables

 @interface NoteTableViewCell : UITableViewCell . . . @property (nonatomic) bool bReadOnly; -(void)overrideConfirmationButtonColor; @end 

我将silyevsk的代码添加到我的.m文件中:

 - (UIView*)recursivelyFindConfirmationButtonInView:(UIView*)view { for(UIView *subview in view.subviews) { if([NSStringFromClass([subview class]) rangeOfString:@"UITableViewCellActionButton"].location != NSNotFound) return subview; UIView *recursiveResult = [self recursivelyFindConfirmationButtonInView:subview]; if(recursiveResult) return recursiveResult; } return nil; } -(void)overrideConfirmationButtonColor { if (!bReadOnly) return; dispatch_async(dispatch_get_main_queue(), ^{ UIView *confirmationButton = [self recursivelyFindConfirmationButtonInView:self]; if(confirmationButton) { confirmationButton.backgroundColor = [UIColor lightGrayColor]; UIImageView* imgPadLock = [[UIImageView alloc] initWithFrame:confirmationButton.frame]; imgPadLock.image = [UIImage imageNamed:@"icnPadlockBlack.png"]; imgPadLock.contentMode = UIViewContentModeCenter; // Don't stretch the UIImage in our UIImageView // Add this new UIImageView in the UIView which contains our Delete button UIView* parent = [confirmationButton superview]; [parent addSubview:imgPadLock]; } }); } 

此外,我需要更改填充UITableView的代码,否则“删除”标签将出现以及挂锁图标:

 -(NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath { Note* note = [listOfNotes objectAtIndex:indexPath.row]; if ( /* Is the user is allowed to delete this cell..? */ ) return @"Delete"; return @" "; } 

这仍然是丑陋的,并依靠假设,当iOS 9出现时,这不会全部改变。

这是解决scheme:

 - (void)layoutSubviews { [super layoutSubviews]; for (UIView *subview in self.subviews) { if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) { UIView *deleteButtonView = (UIView *)[subview.subviews objectAtIndex:0]; UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Background"]]; [deleteButtonView addSubview:image]; } } } 

IOS7兼容的答案(不创build自定义类,但扩展UITableViewCell:

 @implementation UITableViewCell (customdelete) - (void)layoutSubviews { [super layoutSubviews]; for (UIView *subview in self.subviews) { for(UIView *subview2 in subview.subviews){ if ([NSStringFromClass([subview2 class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) { ((UIView*)[subview2.subviews firstObject]).backgroundColor=COLOR_RED; //YOU FOUND THE VIEW, DO WHATEVER YOU WANT, I JUST RECOLOURED IT } } } } @end