【发布时间】:2017-08-15 09:54:07
【问题描述】:
正如我的标题所说,当用户访问 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
})
}
}
通过上面的代码滑动/移动单元格正在工作,但不显示“删除”和“更多”按钮。
所以请指导我正确的方向。
【问题讨论】:
-
您的方法似乎适合左右滑动。但是,我不确定是否可以像这样突出显示内置按钮。您可以确定的是,在您的内容下方放置一个看起来像本机控件的
UIView,滑动以显示它们,然后从单元格中删除/隐藏它们。看看这个答案:stackoverflow.com/questions/5301728/… -
你可以使用这个旧框架:github.com/CEWendel/SWTableViewCell。然后在 viewdidappear 中写:[cell showRightUtilityButtonsAnimated:YES];
-
所以,你的目标是让第一行自动滑动,这样你就可以看到编辑按钮。对吗?
-
@AhmadF - 是的,因为有些时候最终用户不知道此功能,如果我们这样做,他们只能看到列表,然后他们可以很容易地了解某些功能在滑动时可用。
-
这是我使用的另一个解决方案:stackoverflow.com/questions/46881375/…
标签: ios swift uitableview cocoa-touch swipe