在刷行或单击编辑button时,更改UITableViewCell中默认的红色删除button的颜色

我想改变减号button的颜色和删除button的UITableViewCell点击编辑button或刷UITableView行。 到目前为止我已经实现了这个代码:

 -(IBAction)doEdit:(id)sender { [[self keyWordsTable] setEditing:YES animated:NO]; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { } 

iOS 8和9 ( 这个post的道具 )


注意:如果您正在使用现有的iOS 7项目,则需要将目标更新到iOS 8以获取此function。 还记得设置UITableviewDelegate。

现在所有的魔法都发生在这里(尽可能多的button也是你想要的!):

  -(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *button = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Button 1" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { NSLog(@"Action to perform with Button 1"); }]; button.backgroundColor = [UIColor greenColor]; //arbitrary color UITableViewRowAction *button2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Button 2" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { NSLog(@"Action to perform with Button2!"); }]; button2.backgroundColor = [UIColor blueColor]; //arbitrary color return @[button, button2]; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { // you need to implement this method too or nothing will work: } - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } 


(IOS 7)


 **activate the delete button on swipe** // make sure you have the following methods in the 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之前,才会调用替代颜色,所以我们通过重新加载所有东西来确保这种情况发生。)


Swift示例(iOS 8)

UITableViewDelegate文档( editActionsForRowAtIndexPath方法)

返回值

表示该行操作的UITableViewRowAction对象数组。 您提供的每个操作都用于创build用户可以点按的button。

讨论

当您想要为其中一个表格行提供自定义操作时使用此方法。 当用户在一行中水平滑动时,表视图将行内容移到一边以显示您的操作。 点击其中一个动作button执行与动作对象一起存储的处理程序块。

如果不实现此方法,则表格视图在用户滑动行时显示标准附件button。

Swift中的工作示例:

 @available(iOS 8.0, *) func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { let button1 = UITableViewRowAction(style: .Default, title: "Happy!") { action, indexPath in print("button1 pressed!") } button1.backgroundColor = UIColor.blueColor() let button2 = UITableViewRowAction(style: .Default, title: "Exuberant!") { action, indexPath in print("button2 pressed!") } button2.backgroundColor = UIColor.redColor() return [button1, button2] } func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { return true } func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { } 

第一次在.m(customcell)中调用willTransitionToState

 - (void)willTransitionToState:(UITableViewCellStateMask)state{ NSLog(@"EventTableCell willTransitionToState"); [super willTransitionToState:state]; [self overrideConfirmationButtonColor]; } 

检查版本的iOS,在这里,我正在使用iOS 7 – iOS8

 //at least iOS 8 code here - (UIView*)recursivelyFindConfirmationButtonInView:(UIView*)view { if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1) { // iOS 8+ code here 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; } } else{ // Pre iOS 8 code here 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) { UIColor *color = UIColorFromRGB(0xFF7373); confirmationButton.backgroundColor = color; } }); } 

不可以使用公共API。

对于删除button,可以使用自定义实现(如SWTableViewCell )来更改button的颜色以及添加其他button。

老问题,但肯定有一些人支持iOS 7.要更改删除button背景颜色,您需要创build“UITableViewCell”类或扩展它。 那么你可以使用

 - (void)layoutSubviews { [super layoutSubviews]; for (UIView *subview in self.subviews) { for(UIView *childView in subview.subviews){ if ([childView isKindOfClass:[UIButton class]]) { childView.backgroundColor = [UIColor blueColor]; } } } }