Add strikethrough to tableview row with a swipe

2019-07-24 23:42发布

问题:

I've got a problem to strikethrough over the row text while swiping (E.G. left).
I'm using the tableview method leadingSwipeActionsConfigurationForRowAt, but I can't find the solution to strikethrough text while swiping.
As of now, the "swiped" row actually gets deleted.

What I'm looking to do:

Code:

override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let complete = completeAction(at: indexPath)
    return UISwipeActionsConfiguration(actions: [complete])
}

func completeAction(at indexPath: IndexPath) -> UIContextualAction {
    let action = UIContextualAction(style: .destructive, title: "Complete") { (action, view, completion) in
        self.kind.remove(at: indexPath.row)
        self.tableView.deleteRows(at: [indexPath], with: .fade)
                completion(true)
        if let context = (UIApplication.shared.delegate as? AppDelegate)?.coreDataStack.persistentContainer.viewContext {
            let objectToDelete = self.fetchResultsController.object(at: indexPath)
                    context.delete(objectToDelete)
            do {
                try context.save()
            } catch {
                print(error.localizedDescription)
            }
        }
    }
    return action
}

回答1:

I found some solution. Maybe it helps someone. Here is the code:

func strikeThroughText (_ text:String) -> NSAttributedString {
        let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: text)
        attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: 1, range: NSMakeRange(0, attributeString.length))
        return attributeString
    }

UIView.animate(withDuration: 0.1, animations: {
                cell.transform = cell.transform.scaledBy(x: 1.5, y: 1.5)
            }, completion: { (success) in
                UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: .curveEaseOut, animations: {
                    cell.transform = CGAffineTransform.identity
                }, completion: nil)
            })

There is a .gif with the example attributedText