Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swipe-able Table View Cell in iOS 9

I want my table list to have a swipe-able menu like in iOS 8 (first introduced in iOS 7).

Screenshot of table view cell action buttons

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.

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.

like image 299
Dave G Avatar asked Aug 14 '15 07:08

Dave G


People also ask

How do you expand a cell in tableView?

You tap a row (it expands) You tap it again (it contracts)

How do you customize swipe edit buttons in UITableView?

As of iOS 8.0 there's an easy way to customize the list of buttons that appear when the user swipes from right to left: editActionsForRowAt . Return an array of UITableViewRowAction objects that have titles and styles (and also background colors if you want to customize their appearance), and iOS does the rest.


2 Answers

Try this, updated for 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] } 

Also implement this: (You can make it conditional, but here everything is editable)

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {     return true } 
like image 69
jose920405 Avatar answered Oct 19 '22 08:10

jose920405


This code is work for me in the swift4.

enter image description here

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])     } 

enter image description here

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)  } 
like image 24
Niraj Paul Avatar answered Oct 19 '22 09:10

Niraj Paul