Search code examples
iosswiftuitableviewuiswipegesturerecognizer

Change cell content of UITableView in swipe action


I have a label in cell and I want to change the position of the label when swiping the cell.

For example, when swiping cell to left, label will be moved to right. I didn't find any delegate for swiping to add some point in label constraint.


Solution

  • There is UISwipeGestureRecognizer so you can use that for recognition. You can use that for cell, in you cellForRow add that to each cell. Than you can use delegate for the cell to return self. Add leading or trailing constraint of your label as outlet in your cell. You can change constraint like this:

    labelConstraint.constant = 0
         UIView.animateWithDuration(1.0) {
         self.view.layoutIfNeeded()
    }
    

    Edit: If swipe to delete is enabled: Override willTransitionToState method in your custom cell and detect delete state or use tableView's willBeginEditingRowAtIndexPath and change there constant of your constraint