The manipulator for viewing the table in iOS 9

I want my list of tables to have a menu with a choice, for example, in iOS 8 (first appeared in iOS 7).

Screenshot with buttons of a cell cell table

I found a Ray Wenderlich manual that is clear on how to do this, but was written a year and 4 months ago and the code is in Objective-C.

Has iOS 8 or the upcoming iOS 9 finally included this feature in the Apple SDK? I know that they made "swipe to show the delete function", built many years ago. I donโ€™t want to waste my time implementing code with fixed codes to mimic the iOS 8 mail function if Apple iOS is going to transfer it to me in a neatly packaged package.

+79
uitableview ios9 swift custom-cell
Aug 14 '15 at 7:27
source share
8 answers

Try it. (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 } 

(Old version)

 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 } 
+152
Sep 15 '15 at 12:52
source share

You can use the UITableView delegate method to request these actions. Contribute this method as follows:

 - (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]; } 

You can, of course, return a few actions and adjust the color of the text and background.

An implementation of this method is also needed to edit the line:

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { } 
+21
Aug 14 '15 at 8:42
source share

This code works for me in swift4.

enter image description here

The answer on the screen above 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]) } 

enter image description here

Screen answer above: -

  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 the delegate table method similarly: -

 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 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) } 
+21
Jun. 01 '18 at 6:08
source share

I found this MGSwipeTableCell library . After much searching, to implement a slide cell in a table view using swift, I found this and my one line of code to implement and found it extremely useful.

  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 } 

This is the only function you will need to implement and update the pod file.

+15
Oct. 20 '15 at 18:07
source share

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] } } 
+11
Feb 26 '17 at 16:24
source share

AFAIK does not have a built-in ready-made solution, and even if in iOS9, you probably would not be able to use it, since you can not only support iOS9 in your application for the foreseeable future.

Instead, I recommend you study this library:

https://github.com/CEWendel/SWTableViewCell

It is very easy to set up, fully polished and works great in any fast project I worked on.

Hope this helps!

+2
Aug 14 '15 at 7:35
source share

It is easier than you think. Here is an example of a Swift class with implemented UITableView and the ability to navigate 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]) } } 
0
Feb 03 '19 at 21:43
source share

For Swift 4.2 you can see this example:

Link

0
Jul 04 '19 at 14:49
source share



All Articles