可滑动 Table 在 iOS 9 中查看单元格
Swipe-able Table View Cell in iOS 9
我希望我的 table 列表有一个可滑动的菜单,就像 iOS 8 中那样(首次在 iOS 7 中引入)。
我找到了 a Ray Wenderlich guide that is clear 如何做到这一点,但它是一年零 4 个月前写的,代码在 Objective-C.
iOS 8 或即将推出的 iOS 9 是否最终将此功能包含在 Apple 的 SDK 中?我知道他们几年前就内置了 "swipe to reveal delete function"。如果 Apple 的新 iOS 将把它放在一个包装整齐的包裹中交给我,我不想浪费我的时间来实现拼接在一起的代码来模仿 iOS 8 邮件功能。
据我所知,没有内置的现成解决方案,即使 iOS9 中有,您也可能无法使用它,因为您的应用中不能只支持 iOS9可预见的未来。
相反,我建议您看看这个库:
https://github.com/CEWendel/SWTableViewCell
它非常容易配置,非常完善,并且在我参与的任何 swift 项目中都运行良好。
希望对您有所帮助!
您可以使用 UITableView 委托方法来请求这些操作。按如下方式实现此方法:
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *modifyAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Modify" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
// Respond to the action.
}];
modifyAction.backgroundColor = [UIColor blueColor];
return @[modifyAction];
}
您当然可以 return 多项操作并自定义文本和背景颜色。
要使该行可编辑,也需要实施此方法:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
}
试试这个,已更新 Swift 3 (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]
}
也实现这个:(你可以让它有条件,但这里一切都是可编辑的)
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
我找到了这个图书馆 MGSwipeTableCell
在搜索了很多使用 swift 在 table 视图中实现幻灯片单元格之后,我发现了这个及其仅一行代码来实现并发现它非常有用。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let reuseIdentifier = "programmaticCell"
var cell = self.table.dequeueReusableCellWithIdentifier(reuseIdentifier) as! MGSwipeTableCell!
if cell == nil
{
cell = MGSwipeTableCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: reuseIdentifier)
}
cell.textLabel!.text = "Title"
cell.detailTextLabel!.text = "Detail text"
cell.delegate = self //optional
//configure left buttons
cell.leftButtons = [MGSwipeButton(title: "", icon: UIImage(named:"check.png"), backgroundColor: UIColor.greenColor())
,MGSwipeButton(title: "", icon: UIImage(named:"fav.png"), backgroundColor: UIColor.blueColor())]
cell.leftSwipeSettings.transition = MGSwipeTransition.Rotate3D
//configure right buttons
cell.rightButtons = [MGSwipeButton(title: "Delete", backgroundColor: UIColor.redColor())
,MGSwipeButton(title: "More",backgroundColor: UIColor.lightGrayColor())]
cell.rightSwipeSettings.transition = MGSwipeTransition.Rotate3D
return cell
}
这是您必须实施和更新 pod 文件的唯一功能
Swift 3个完整的解决方案:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.tableFooterView = UIView(frame: CGRect.zero) //Hiding blank cells.
tableView.separatorInset = UIEdgeInsets.zero
tableView.dataSource = self
tableView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath)
return cell
}
//Enable cell editing methods.
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let more = UITableViewRowAction(style: .normal, title: "More") { action, index in
//self.isEditing = false
print("more button tapped")
}
more.backgroundColor = UIColor.lightGray
let favorite = UITableViewRowAction(style: .normal, title: "Favorite") { action, index in
//self.isEditing = false
print("favorite button tapped")
}
favorite.backgroundColor = UIColor.orange
let share = UITableViewRowAction(style: .normal, title: "Share") { action, index in
//self.isEditing = false
print("share button tapped")
}
share.backgroundColor = UIColor.blue
return [share, favorite, more]
}
}
这段代码适用于我的 swift4。
Answer of the above screen is:-
func tableView(_ tableView: UITableView,
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
// Write action code for the trash
let TrashAction = UIContextualAction(style: .normal, title: "Trash", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
success(true)
})
TrashAction.backgroundColor = .red
// Write action code for the Flag
let FlagAction = UIContextualAction(style: .normal, title: "Flag", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
success(true)
})
FlagAction.backgroundColor = .orange
// Write action code for the More
let MoreAction = UIContextualAction(style: .normal, title: "More", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
success(true)
})
MoreAction.backgroundColor = .gray
return UISwipeActionsConfiguration(actions: [TrashAction,FlagAction,MoreAction])
}
Answer of the above screen:-
func tableView(_ tableView: UITableView,
leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
let closeAction = UIContextualAction(style: .normal, title: "Mark as Read", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("CloseAction ...")
success(true)
})
closeAction.backgroundColor = .blue
return UISwipeActionsConfiguration(actions: [closeAction])
}
Write tableview Delegate method likewise:-
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrPerson.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let personName = arrPerson[indexPath.row]
cell.textLabel?.text = personName.personName
return cell
}
And in the viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
tblView.delegate = self
tblView.dataSource = self
let person1 = personData(personName: "Jonny", personAge: 30)
let person2 = personData(personName: "Chandan", personAge: 20)
let person3 = personData(personName: "Gopal", personAge: 28)
arrPerson.append(person1)
arrPerson.append(person2)
arrPerson.append(person3)
}
这比你想象的要容易。
下面是一个 Swift class 的示例,它具有已实现的 UITableView 和滑动 UITableViewCell 的能力。
import UIKit
class ViewController: UIViewController {
// MARK: Properties
let strings = ["firstString", "secondString", "thirdString"]
// MARK: Outlets
@IBOutlet weak var tableView: UITableView!
// MARK: Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
}
extension ViewController: UITableViewDataSource, UITableViewDelegate {
// MARK: UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objects.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath)
let currentString = strings[indexPath.row]
cell.textLabel?.text = currentString
return cell
}
// MARK: UITableViewDelegate
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let leftAction = UIContextualAction(style: .normal, title: "Red", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("leftAction tapped")
success(true)
})
leftAction.image = UIImage(named: "")
leftAction.backgroundColor = UIColor.red
return UISwipeActionsConfiguration(actions: [leftAction])
}
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let rightAction = UIContextualAction(style: .normal, title: "Green", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("rightAction tapped")
success(true)
})
rightAction.image = UIImage(named: "")
rightAction.backgroundColor = UIColor.green
return UISwipeActionsConfiguration(actions: [rightAction])
}
}
我希望我的 table 列表有一个可滑动的菜单,就像 iOS 8 中那样(首次在 iOS 7 中引入)。
我找到了 a Ray Wenderlich guide that is clear 如何做到这一点,但它是一年零 4 个月前写的,代码在 Objective-C.
iOS 8 或即将推出的 iOS 9 是否最终将此功能包含在 Apple 的 SDK 中?我知道他们几年前就内置了 "swipe to reveal delete function"。如果 Apple 的新 iOS 将把它放在一个包装整齐的包裹中交给我,我不想浪费我的时间来实现拼接在一起的代码来模仿 iOS 8 邮件功能。
据我所知,没有内置的现成解决方案,即使 iOS9 中有,您也可能无法使用它,因为您的应用中不能只支持 iOS9可预见的未来。
相反,我建议您看看这个库:
https://github.com/CEWendel/SWTableViewCell
它非常容易配置,非常完善,并且在我参与的任何 swift 项目中都运行良好。
希望对您有所帮助!
您可以使用 UITableView 委托方法来请求这些操作。按如下方式实现此方法:
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *modifyAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Modify" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
// Respond to the action.
}];
modifyAction.backgroundColor = [UIColor blueColor];
return @[modifyAction];
}
您当然可以 return 多项操作并自定义文本和背景颜色。
要使该行可编辑,也需要实施此方法:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
}
试试这个,已更新 Swift 3 (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]
}
也实现这个:(你可以让它有条件,但这里一切都是可编辑的)
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
我找到了这个图书馆 MGSwipeTableCell 在搜索了很多使用 swift 在 table 视图中实现幻灯片单元格之后,我发现了这个及其仅一行代码来实现并发现它非常有用。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let reuseIdentifier = "programmaticCell"
var cell = self.table.dequeueReusableCellWithIdentifier(reuseIdentifier) as! MGSwipeTableCell!
if cell == nil
{
cell = MGSwipeTableCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: reuseIdentifier)
}
cell.textLabel!.text = "Title"
cell.detailTextLabel!.text = "Detail text"
cell.delegate = self //optional
//configure left buttons
cell.leftButtons = [MGSwipeButton(title: "", icon: UIImage(named:"check.png"), backgroundColor: UIColor.greenColor())
,MGSwipeButton(title: "", icon: UIImage(named:"fav.png"), backgroundColor: UIColor.blueColor())]
cell.leftSwipeSettings.transition = MGSwipeTransition.Rotate3D
//configure right buttons
cell.rightButtons = [MGSwipeButton(title: "Delete", backgroundColor: UIColor.redColor())
,MGSwipeButton(title: "More",backgroundColor: UIColor.lightGrayColor())]
cell.rightSwipeSettings.transition = MGSwipeTransition.Rotate3D
return cell
}
这是您必须实施和更新 pod 文件的唯一功能
Swift 3个完整的解决方案:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.tableFooterView = UIView(frame: CGRect.zero) //Hiding blank cells.
tableView.separatorInset = UIEdgeInsets.zero
tableView.dataSource = self
tableView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath)
return cell
}
//Enable cell editing methods.
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let more = UITableViewRowAction(style: .normal, title: "More") { action, index in
//self.isEditing = false
print("more button tapped")
}
more.backgroundColor = UIColor.lightGray
let favorite = UITableViewRowAction(style: .normal, title: "Favorite") { action, index in
//self.isEditing = false
print("favorite button tapped")
}
favorite.backgroundColor = UIColor.orange
let share = UITableViewRowAction(style: .normal, title: "Share") { action, index in
//self.isEditing = false
print("share button tapped")
}
share.backgroundColor = UIColor.blue
return [share, favorite, more]
}
}
这段代码适用于我的 swift4。
Answer of the above screen is:-
func tableView(_ tableView: UITableView,
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
// Write action code for the trash
let TrashAction = UIContextualAction(style: .normal, title: "Trash", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
success(true)
})
TrashAction.backgroundColor = .red
// Write action code for the Flag
let FlagAction = UIContextualAction(style: .normal, title: "Flag", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
success(true)
})
FlagAction.backgroundColor = .orange
// Write action code for the More
let MoreAction = UIContextualAction(style: .normal, title: "More", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
success(true)
})
MoreAction.backgroundColor = .gray
return UISwipeActionsConfiguration(actions: [TrashAction,FlagAction,MoreAction])
}
Answer of the above screen:-
func tableView(_ tableView: UITableView,
leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
let closeAction = UIContextualAction(style: .normal, title: "Mark as Read", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("CloseAction ...")
success(true)
})
closeAction.backgroundColor = .blue
return UISwipeActionsConfiguration(actions: [closeAction])
}
Write tableview Delegate method likewise:-
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrPerson.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let personName = arrPerson[indexPath.row]
cell.textLabel?.text = personName.personName
return cell
}
And in the viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
tblView.delegate = self
tblView.dataSource = self
let person1 = personData(personName: "Jonny", personAge: 30)
let person2 = personData(personName: "Chandan", personAge: 20)
let person3 = personData(personName: "Gopal", personAge: 28)
arrPerson.append(person1)
arrPerson.append(person2)
arrPerson.append(person3)
}
这比你想象的要容易。 下面是一个 Swift class 的示例,它具有已实现的 UITableView 和滑动 UITableViewCell 的能力。
import UIKit
class ViewController: UIViewController {
// MARK: Properties
let strings = ["firstString", "secondString", "thirdString"]
// MARK: Outlets
@IBOutlet weak var tableView: UITableView!
// MARK: Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
}
extension ViewController: UITableViewDataSource, UITableViewDelegate {
// MARK: UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objects.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath)
let currentString = strings[indexPath.row]
cell.textLabel?.text = currentString
return cell
}
// MARK: UITableViewDelegate
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let leftAction = UIContextualAction(style: .normal, title: "Red", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("leftAction tapped")
success(true)
})
leftAction.image = UIImage(named: "")
leftAction.backgroundColor = UIColor.red
return UISwipeActionsConfiguration(actions: [leftAction])
}
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let rightAction = UIContextualAction(style: .normal, title: "Green", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("rightAction tapped")
success(true)
})
rightAction.image = UIImage(named: "")
rightAction.backgroundColor = UIColor.green
return UISwipeActionsConfiguration(actions: [rightAction])
}
}