Josh O'Connor
Josh O'Connor

Reputation: 4962

Swipe to delete entire section in UITableView (iOS)

I've had a lot of success in the past using MGSwipeTableCell to swipe to dismiss cells, but my current task calls to swipe an entire section in the same behavior.

I currently have a swipe gesture recognizer in the UITableView, when the swipe gesture is triggered, I calculate the section the touch was recieved, and delete the objects that populate that section (in core data), then call the delete animation:

//Delete objects that populate table datasource 
for notification in notifications {
    notificationObject.deleted = true
}

DataBaseManager.sharedInstance.save()

let array = indexPathsToDelete
let indexSet = NSMutableIndexSet()
array.forEach(indexSet.add)

//Delete section with animation            
self.notificationsTableView.deleteSections(indexSet as IndexSet, with: .left)

This works, but is not ideal. Ideally we would like the whole section to drag with your finger (and when released at a certain point, it goes off screen), similar to MGSwipeTableCell. What is the best way to approach this? Is there another library which allows swipe to delete sections (I can't find any)? Or is this something I will have to create myself.

Upvotes: 3

Views: 1277

Answers (2)

Josh O'Connor
Josh O'Connor

Reputation: 4962

Brandon's answer is correct, however, INSPullToRefresh library has issues when using touches began and other touch delegate methods.

What I had to do was implement a UIPanGestureRecognizer and track the touch when that gesture recognizer event is fired

Upvotes: 0

Brandon A
Brandon A

Reputation: 8279

I haven't tested this but the idea is below. Take a view (self.header) and use the touchesBegan... method to detect the user placing their finger on screen. Then, follow the finger with the touchesMoved... method and calculate the difference between the last offset and the next. It should grow by 1 (or more) depending on how fast the user is moving their finger. Use this value to subtract the origin.x of the cell's contentView.

var header: UIView!
var tableView:UITableView!
var offset:CGFloat = 0

override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    // Touches Began. Disable user activity on UITableView
    if let touch = touches.first {
        // Get the point where the touch started
        let point = touch.location(in: self.header)
        offset = point.x
    }
}

override public func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {

        // Get the point where the touch is moving in header
        let point = touch.location(in: self.header)

        // Calculate the movement of finger
        let x:CGFloat = offset - point.x
        if x > 0 {
            // Move cells by offset
            moveCellsBy(x: x)
        }

        // Set new offset
        offset = point.x
    }
}

override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    // Reset offset when user lifts finter
    offset = 0
}

func moveCellsBy(x: CGFloat) {
    // Move each visible cell with the offset
    for cell in self.tableView.visibleCells {
        // Place in animation block for smoothness
        UIView.animate(withDuration: 0.05, animations: {
            cell.contentView.frame = CGRect(x: cell.contentView.frame.origin.x - x, y: cell.contentView.frame.origin.y, width: cell.contentView.frame.size.width, height: cell.contentView.frame.size.height)
        })
    }
}

Upvotes: 2

Related Questions