3

I want to implement this code in Swift. I got the following code for it in Objective-C from these questions:

Change the color of default red color delete button in UITableViewCell when swiping rows or click on edit button

Customize the delete button in UITableView

- (void)willTransitionToState:(UITableViewCellStateMask)state{
[super willTransitionToState:state];
if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask) {
    for (UIView *subview in self.subviews) {
        if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) {
            UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
            [deleteBtn setImage:[UIImage imageNamed:@"delete.png"]];
            [[subview.subviews objectAtIndex:0] addSubview:deleteBtn];
            [deleteBtn release];
        }
    }
}
}

I don't know how to implement this method in Swift. Could anyone help me?

I am using Xcode 7.3 Beta.

Community
  • 1
  • 1
Kautham Krishna
  • 967
  • 1
  • 14
  • 33

2 Answers2

6

swift tableview delegate have new method. Try this may be it will resolve your problem.

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?{

    let ackAction = UITableViewRowAction(style: .default, title: "Himanshu", handler: myFunction)
        ackAction.backgroundColor = UIColor.orange

    return [ackAction]
}

Now you can even modify your delete functionality

Capella
  • 881
  • 3
  • 19
  • 32
  • 1
    thanks for your answer.This helped me a lot. Sorry for doing it late. I replaced the second line to `deleteButton.backgroundColor = UIColor(patternImage: UIImage(named: "delete_button")!)`. and it worked like a gem. – Kautham Krishna Apr 21 '16 at 09:21
-3
UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
            [deleteBtn setImage:[UIImage imageNamed:@"delete.png"]];

Here you see your button delete.png you need to change delete.png in your images. later will change.

[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];

Also here you can change dimensions

Thanks

SwiftDeveloper
  • 7,244
  • 14
  • 56
  • 85