【问题标题】:Swift: Swipe action to strikethrough row in TableViewSwift:在 TableView 中滑动操作以删除删除线行
【发布时间】:2019-05-18 11:30:05
【问题描述】:

晚安,女士们,先生们, 我目前正在习惯 Swift,并想从一个小 todo 应用程序开始。到目前为止,我可以添加一个项目并在上下文中持久地保护它。添加项目后,它将显示在表格视图中。现在,我想使用检查滑动来删除已添加的项目,并在我的上下文中保护这些信息。使用滑动删除非常好。

有人知道如何实现这一点吗?我试图自己解决它,但无法完成。之前在这里问过类似的问题,但没有得到正确的答案:Add strikethrough to tableview row with a swipe

func checkAccessoryType(cell: UITableViewCell, isCompleted: Bool) {
    if isCompleted {
        cell.accessoryType = .checkmark

    } else {
        cell.accessoryType = .none
    }
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let todo = CoreDataManager.shared.getTodoItem(index: indexPath.row)
    todo.completed = !todo.completed
    CoreDataManager.shared.safeContext()

    if let cell = tableView.cellForRow(at: indexPath){
        checkAccessoryType(cell: cell, isCompleted: todo.completed)
    }
}

【问题讨论】:

    标签: ios swift uitableview tableview strikethrough


    【解决方案1】:

    假设您尝试删除任务的标题(应定义为标签),请采用以下方法:

    1- 确保您的标签设置为属性文本而不是普通文本。为此,请转到 Main.storyboard,选择您的标签,然后在属性检查器中,将文本设置为 Attributed。

    2- 在您的完成块(即滑动后执行的完成块)内添加以下代码:

    SWIFT 5

    let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: taskLabel.text)
    attributeString.addAttribute(.strikethroughStyle, value: 1, range: NSRange(location: 0, length: taskLabel.text.count))
    taskLabel.attributedText = attributeString
    

    只是一点建议:如果您在提出问题时添加一些代码,总是很有帮助的。 如果有什么不明白的地方请告诉我。

    【讨论】:

    • 嘿,我在我的问题中添加了用于滑动的代码行。我已经完成了您建议的解决方案的第一步,但我不确定您的完成块是什么意思?
    • 嘿,完成块是在您的单元格被刷卡后执行的代码块。所以基本上,在我添加打印语句的 'in' 关键字之后添加代码:let action = UIContextualAction(style: .normal, title: "Check") { (action, view, completion) in print("This is the completion block") }
    • 您好,非常感谢您的回答。我在 MainStoryboard 中将标签标识符设置为 taskLabel。但是当我粘贴你的代码时,xCode 会抛出一个错误,上面写着:使用未解析的标识符“taskLabel”。与标签所示的字符串是使用核心数据生成的有关系吗?
    • 嘿,没问题。您是否在 ViewController 中创建了 IBOutlet?
    • 我想是的。我将表格视图与带有插座的视图控制器连接起来:@IBOutlet var todoTableView: UITableView!
    【解决方案2】:

    查看您提供的链接,您需要在 UITableViewCell 上滑动操作

    尝试调查:

    • leadingSwipeActionsConfigurationForRowAt
    • trailingSwipeActionsConfigurationForRowAt

    您需要此操作来执行删除线标签或删除:

    func tableView(_ tableView: UITableView,
                    leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
     {
         let closeAction = UIContextualAction(style: .normal, title:  "Close", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
                 print("OK, marked as Closed")
                 success(true)
             })
             closeAction.image = UIImage(named: "tick")
             closeAction.backgroundColor = .purple
    
             return UISwipeActionsConfiguration(actions: [closeAction])
    
     }
    
     func tableView(_ tableView: UITableView,
                    trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
     {
         let modifyAction = UIContextualAction(style: .normal, title:  "Update", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
             print("Update action ...")
             success(true)
         })
         modifyAction.image = UIImage(named: "hammer")
         modifyAction.backgroundColor = .blue
    
         return UISwipeActionsConfiguration(actions: [modifyAction])
     }
    

    来源:https://developerslogblog.wordpress.com/2017/06/28/ios-11-swipe-leftright-in-uitableviewcell/

    【讨论】:

    • 不幸的是,当我使用您提供的代码执行删除滑动时,该应用程序崩溃了。我正在使用核心数据在表格视图中生成、保存和加载我的待办事项。这可能与此有关吗?
    猜你喜欢
    • 2012-03-17
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 2018-10-26
    • 1970-01-01
    • 2017-06-23
    相关资源
    最近更新 更多