Search code examples
iosuitableviewuitableviewrowaction

Is there any way to change "DELETE" button from UITableViewCell?


I want to change DELETE button in UITableViewCell. Actually, I need to :

  1. Change position of button
  2. font
  3. text color of that button.

Here I can only change button text in following delegate method :

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    UITableViewRowAction *viewAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Follow" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {  

        NSLog(@"View Action fired");  
    }];  
    viewAction.backgroundColor = [UIColor clearColor];  

    return @[callAction];  
}  

But how to change other attributes of button. If anyone knows, please let me know.

Thanks.


Solution

  • To get this working, implement the following two methods on your UITableView's delegate to get the desired effect

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return YES;
    }
    
     - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // you need to implement this method too esle nothing will work:
    
    }
    

    check your method you created the name of UITableViewRowAction *viewAction but you returned teh value of callAction modify once and try

    - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath  
    {  
        UITableViewRowAction *callAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Follow" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {  
    
            NSLog(@"View Action fired");  
        }];  
        callAction.backgroundColor = [UIColor clearColor];  
    
        return @[callAction];  
    }  
    

    In Standard UITableViewRowAction we can't change , we can change only

    style

    title

    backgroundColor

    backgroundEffect

    for more information see the Apple Documents