iOS 9中可滑动的表格视图单元格 [英] Swipe-able Table View Cell in iOS 9
问题描述
我希望我的表列表具有可滑动菜单,例如iOS 8(iOS 7中首次引入).
I want my table list to have a swipe-able menu like in iOS 8 (first introduced in iOS 7).
I've found a Ray Wenderlich guide that is clear on how to do it, but it was written a year and 4 months ago and the code is in Objective-C.
iOS 8或即将推出的iOS 9最终是否在Apple的SDK中包含此功能?我知道他们在几年前内置了滑动显示删除功能".如果Apple的新iOS打算将它打包成整齐的包装交给我,我不想浪费时间实施补丁代码来模仿iOS 8邮件功能.
Did iOS 8 or the upcoming iOS 9 finally include this function in Apple's SDK? I know they made the "swipe to reveal delete function" built-in years ago. I don't want to waste my time implementing patched-together code to mimic the iOS 8 mail function, if Apple's new iOS is going to hand it to me in a neatly wrapped package.
推荐答案
尝试一下. (已针对Swift 3.0更新)(开发人员文档)
Try this. (Updated for Swift 3.0) (Developer Docs)
override func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
let more = UITableViewRowAction(style: .normal, title: "More") { action, index in
print("more button tapped")
}
more.backgroundColor = .lightGray
let favorite = UITableViewRowAction(style: .normal, title: "Favorite") { action, index in
print("favorite button tapped")
}
favorite.backgroundColor = .orange
let share = UITableViewRowAction(style: .normal, title: "Share") { action, index in
print("share button tapped")
}
share.backgroundColor = .blue
return [share, favorite, more]
}
也可以实现这一点:(您可以将其设置为有条件,但是这里的所有内容都是可编辑的)
Also implement this: (You can make it conditional, but here everything is editable)
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
(旧版本)
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let more = UITableViewRowAction(style: .Normal, title: "More") { action, index in
print("more button tapped")
}
more.backgroundColor = UIColor.lightGrayColor()
let favorite = UITableViewRowAction(style: .Normal, title: "Favorite") { action, index in
print("favorite button tapped")
}
favorite.backgroundColor = UIColor.orangeColor()
let share = UITableViewRowAction(style: .Normal, title: "Share") { action, index in
print("share button tapped")
}
share.backgroundColor = UIColor.blueColor()
return [share, favorite, more]
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// the cells you would like the actions to appear needs to be editable
return true
}
这篇关于iOS 9中可滑动的表格视图单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!