Swipe-able Table View Cell in iOS 9
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}
(Older 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 }
This code is work for me in the 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)}
You can use a UITableView delegate method to ask for those actions. Implement 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 multiple actions and customize the text and background color.
Implementing this method is also required to make the row editable:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {}