Add strikethrough to tableview row with a swipe

…衆ロ難τιáo~ 提交于 2019-12-24 06:17:14

问题


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



来源:https://stackoverflow.com/questions/50276214/add-strikethrough-to-tableview-row-with-a-swipe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!