在UITableViewCell上向右滑动

时间:2016-01-22 08:05:27

标签: ios swift uitableview swipe

所以我已经在网站上搜索了这个问题的答案,并且有一些不错的结果,但是由于XCode 7不再处于测试阶段,因此swift 2.0现在已成为标准。

我已使用以下代码进行扫描,以便向左滑动'功能将导致UITableViewCell发生一些事情 -

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){

        if editingStyle == UITableViewCellEditingStyle.Delete {
       ....
       }
}

据我所知,这是Apple现在提供的API,并且不需要使用外部类。

我也了解到您可以使用此本机代码自定义此滑动所产生的操作:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {

        let more = UITableViewRowAction(style: .Normal, title: "More") { action, index in
           print("more button tapped")
}

我的问题是,是否还有任何本机代码(iOS 9.0 +,swift 2.0)可以定义一个正确的滑动'在UITableViewCell上?

提前谢谢你!

3 个答案:

答案 0 :(得分:3)

    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])
 }

答案 1 :(得分:2)

不,现在没有这样的原生API。

答案 2 :(得分:1)

此功能在iOS 11.0+和Mac Catalyst 13.0+上可用。对于左侧的选项,请使用前导,而在右侧的则使用尾随方法。

只需实现UITableViewDelegate方法并返回UISwipeActionsConfiguration对象。您可以选择包含图像或仅文本作为按钮的标题。

func tableView(_ tableView: UITableView, 
leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?

func tableView(_ tableView: UITableView, 
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?

确保在数据源委托中启用编辑,否则将禁用滑动选项。

// True for enabling the editing mode
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
      return true
    }
相关问题