正如我的标题所示,当用户访问UITableView
时,我想从左向右滑动第一行ViewController
。
在我的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
})
}
}
通过上面的代码滑动/移动单元工作但不显示“删除”和“更多”按钮。
所以请指导我正确的方向。
答案 0 :(得分:5)
我找到了一种方法来实现它。
Semaphore
在单元格的末尾添加自定义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
中调用此代码时,只应调用一次用户指示。
答案 1 :(得分:1)
向左转换单元格不会使操作按钮可见。
您需要通过调用单元格上的entities
方法将该单元格发送到编辑模式,并将其放入setEditing
:
viewDidAppear