ios - 无需触摸即可滑动 UITableViewCell

标签 ios swift uitableview cocoa-touch swipe

正如我的标题所说,当用户访问 ViewController 时,我想从左向右滑动 UITableView 的第一行。

在我的 ViewController 中,我有一个 UITableView,每一行都有两个按钮“更多”和“删除”操作。看下面的代码

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.delete) {
    }
}


func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let deleteButton = UITableViewRowAction(style: .normal, title: "Delete") { action, index in
        // Edit Button Action
    }
    deleteButton.backgroundColor = UIColor.red

    let editButton = UITableViewRowAction(style: .normal, title: "Edit") { action, index in
        // Delete Button Action
    }
    editButton.backgroundColor = UIColor.lightGray

    return [deleteButton, editButton]
}

一切正常。但我希望当最终用户第一次访问此 ViewController 时,他们会通知有可用的滑动操作,以便他们执行它。

问题是:第一行如何自动左右滑动?

我做了什么?

 override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let cell = posTblView.cellForRow(at: NSIndexPath(row: 0, section: 0) as IndexPath) as! POSUserTabelCell
    UIView.animate(withDuration: 0.3, animations: {
        cell.transform = CGAffineTransform.identity.translatedBy(x: -150, y: 0)
    }) { (finished) in
        UIView.animateKeyframes(withDuration: 0.3, delay: 0.25, options: [], animations: {
            cell.transform = CGAffineTransform.identity
        }, completion: { (finished) in
        })
    }
}

通过上面的代码滑动/移动单元格是有效的,但不显示“删除”和“更多”按钮。

所以请指导我正确的方向。

最佳答案

我找到了一种实现它的方法。

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    if arrStudent.count > 0 {
        let indexPath = NSIndexPath(row: 0, section: 0)
        let cell = tblStudent.cellForRow(at: indexPath as IndexPath);

        let swipeView = UIView()
        swipeView.frame = CGRect(x: cell!.bounds.size.width, y: 0, width: 170, height: cell!.bounds.size.height)
        swipeView.backgroundColor = .clear

        let swipeEditLabel: UILabel = UILabel.init(frame: CGRect(x: 0, y: 0, width: 80, height: cell!.bounds.size.height))
        swipeEditLabel.text = "Edit";
        swipeEditLabel.textAlignment = .center
        swipeEditLabel.font = UIFont.systemFont(ofSize: 19)
        swipeEditLabel.backgroundColor = UIColor(red: 180/255, green: 180/255, blue: 180/255, alpha: 1) // Light Gray: Change color as you want
        swipeEditLabel.textColor = UIColor.white
        swipeView.addSubview(swipeEditLabel)

        let swipeDeleteLabel: UILabel = UILabel.init(frame: CGRect(x: swipeEditLabel.frame.size.width, y: 0, width: 90, height: cell!.bounds.size.height))
        swipeDeleteLabel.text = "Delete";
        swipeDeleteLabel.textAlignment = .center
        swipeDeleteLabel.font = UIFont.systemFont(ofSize: 19)
        swipeDeleteLabel.backgroundColor = UIColor(red: 255/255, green: 41/255, blue: 53/255, alpha: 1) // Red color: Change color as you want
        swipeDeleteLabel.textColor = UIColor.white
        swipeView.addSubview(swipeDeleteLabel)

        cell!.addSubview(swipeView)

        UIView.animate(withDuration: 0.60, animations: {
            cell!.frame = CGRect(x: cell!.frame.origin.x - 170, y: cell!.frame.origin.y, width: cell!.bounds.size.width + 170, height: cell!.bounds.size.height)
        }) { (finished) in
            UIView.animate(withDuration: 0.60, animations: {
                cell!.frame = CGRect(x: cell!.frame.origin.x + 170, y: cell!.frame.origin.y, width: cell!.bounds.size.width - 170, height: cell!.bounds.size.height)

            }, completion: { (finished) in
                for subview in swipeView.subviews {
                    subview.removeFromSuperview()
                }
                swipeView.removeFromSuperview()
            })

        }
    }
}

在单元格末尾添加自定义 UIView 并在动画完成后将其移除。

  • 您可以根据需要设置自定义 View 的框架
  • 根据需要添加带有背景颜色和标题的标签。

在这里你应该编写代码,而不是每次在 viewDidAppear 中调用此代码,它应该只调用一次以供用户指示。

关于ios - 无需触摸即可滑动 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45690519/

相关文章:

ios - 如何按数据添加顺序获取核心数据并按该顺序显示在 UITableView 中?

ios - 自定义 UITableViewCell accessoryView 按钮在真实设备上不可见

ios - Swift 'defer' 关键字导致段错误

ios - 在 iOS 上创建图像并尝试将图像分配给 UIImageView 导致 EXC_BAD_ACCESS

ios - UIScrollview 分页中的 UITextview

arrays - 如何在 swift 3 中将带有自定义类的数组转换为 JSON

ios - 找不到 Google 跟踪代码管理器容器 ios swift

iOS "UITemporaryLayoutHeight"约束

ios - 如何在 UITableViewCell 上显示数组数据?

iphone - 如何在拍摄照片的那一刻暂停视频?