在滑动行或单击编辑按钮时,更改UITableViewCell中默认红色删除按钮的颜色

Hus*_*bir 37 iphone uitableview ios

我想UITableViewCell在点击编辑按钮或滑动UITableView行时更改减号按钮的颜色并删除按钮.到目前为止,我已实现此代码:

-(IBAction)doEdit:(id)sender
{

    [[self keyWordsTable] setEditing:YES animated:NO];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {

}
Run Code Online (Sandbox Code Playgroud)

tim*_*ykc 98


iOS 8和9(这篇文章的道具)


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

所有的魔法现在都在这里发生(你想要的按钮也很多!!!!):

 -(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;
    }
Run Code Online (Sandbox Code Playgroud)

(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.");
    }
Run Code Online (Sandbox Code Playgroud)

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

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"Your Label";
}
Run Code Online (Sandbox Code Playgroud)

为按钮第1部分设置自定义颜色 - 警告,这在技术上涉及戳私人苹果API.但是,不会阻止使用属于UIKIT的公共方法搜索来修改子视图.

创建一个uitableviewcell类(另请参阅/sf/answers/1564557221/)

- (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];
            }
        }
    }    
}
Run Code Online (Sandbox Code Playgroud)

另一个注意事项:无法保证此方法在将来的更新中有效.还要注意提及或使用私有UITableViewCellDeleteConfirmationView类可能导致AppStore拒绝.

为按钮部分2设置自定义颜色

回到你的uitableviewcontroller

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    [YourTableView reloadData];
}
Run Code Online (Sandbox Code Playgroud)

(直到下次在tablecell上调用layoutSubviews时才会调用备用颜色,因此我们通过重新加载所有内容来确保这种情况发生.)


cle*_*ght 23


Swift示例(iOS 8)

UITableViewDelegate docs(editActionsForRowAtIndexPath方法)

回报价值

一组UITableViewRowAction对象,表示行的操作.您提供的每个操作都用于创建用户可以点按的按钮.

讨论

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

如果未实现此方法,则表视图会在用户滑动行时显示标准附件按钮.

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) {
}
Run Code Online (Sandbox Code Playgroud)

  • 还有减号按钮(这也被问到了)? (2认同)