MortimerGoro / MGSwipeTableCell

An easy to use UITableViewCell subclass that allows to display swippable buttons with a variety of transitions.
MIT License
6.97k stars 1.08k forks source link

how to manually expand a cell? #331

Open sciamano89 opened 5 years ago

sciamano89 commented 5 years ago

I'm in a situation where I reload a tableView, and if a cell was being swiped before the reload, I want to restore its overall state right after the reload.

I'm currently achieving this with:

            //check if we have a swiped cell, and store its current values
            var swipedCellInfo: (indexPath: IndexPath, offset: CGFloat, state: MGSwipeState)?

            if let cell = tableView.visibleCells.first(where: {($0 as? MGSwipeTableCell)?.swipeState != MGSwipeState.none }) as? MGSwipeTableCell, let swipedIndexPath = tableView.indexPath(for: cell)
            {
                swipedCellInfo = (swipedIndexPath, cell.swipeOffset, cell.swipeState)
            }

            //reload data
            tableView.reloadData()

            //if we were swiping a cell, restore it to its state before the reload
            if let info = swipedCellInfo, let swipedCell = (tableView.cellForRow(at: info.indexPath) as? MGSwipeTableCell), let buttonWidth = swipedCell.rightButtons.first?.bounds.width
            {
                swipedCell.setSwipeOffset(info.offset, animated: false)

                if info.state == MGSwipeState.expandingRightToLeft
                {
                    swipedCell.expandSwipe(.rightToLeft, animated: false)
                    swipedCell.showSwipe(.rightToLeft, animated: true)
                }
                else
                {
                    //if swipe revealed all rightButtons or went over, go back to showing swipe
                    if info.offset <= (-buttonWidth * CGFloat(swipedCell.rightButtons.count))
                    {
                        swipedCell.showSwipe(.rightToLeft, animated: true)
                    }
                    else //hide swipe
                    {
                        swipedCell.hideSwipe(animated: true)
                    }
                }
            }

It works like I want it when the cell is not expanding, (restoring its swipeOffset works perfectly) but the moment the cell is expanding, the animation becomes weird. First of all, "expandSwipe()" still animates even though animate is set to false. Is there a way to manually set the expansion offset? And a way to resume the swipe gesture? (reload data interrupts the gesture).

Thanks :)