editActionsForRowAtIndexPath for one section iOS Swift - ios

As you can see in the images, there are two sections in this tableView.
May I know how to manage to enable the editActionsForRowAtIndexPath in section one only?
Here is my code that enable the tableViewCell to have a row swipe...
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?
{
let complete = UITableViewRowAction(style: .Normal, title: "Complete")
{ action, index in
print("more button tapped")
}
complete.backgroundColor = UIColor.greenColor()
return [complete]
}

You can use indexPath.section to decide which section you are setting, but you should use tableView:canEditRowAtIndexPath:indexPath: to tell the table view when you want to enable editing.
Swift 2
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool{
return indexPath.section == 0
}
Swift 3
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return indexPath.section == 0
}

Related

Change the text of the cell delete button

i'm trying to change the delete button of a cell.
I have 2 functions :
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let titleBtn = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: "Supprimer") { (action , indexPath ) -> Void in
self.isEditing = false
//ackAction.backgroundColor = UIColor.orange
}
return [titleBtn]
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
But, when I run my app, the text of the button is changed but the delete doesn't work (i can't delete data from my array and the row of my tableview). Before to add this functions all worked perfectly.
A detail: in the canEditRowAt function, I tried to return false too...
Thanks by advance
if you want to change the text of the delete button, conform this method in the UITableViewDelegate:
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String?
{
return "Your new title"
}
To delete the item from the array conform this method
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
if (editingStyle == UITableViewCellEditingStyle.delete)
{
yourDataSourceArray.remove(at: indexPath.row)
yourTableView.reloadData()
}
}
You can either use default delete action and change its title like this.
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String?
{
return "MyAction"
}
or you can create your own action buttons like this.
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteAction = UITableViewRowAction(style: .default, title: "Delete") {action in
//handle delete
}
let editAction = UITableViewRowAction(style: .normal, title: "Edit") {action in
//handle edit
}
return [deleteAction, editAction]
}

Is there a way of implementing editActionsForRowAtIndexPath only for a specific UITableViewCell in Swift?

I'm implementing a UITableViewController in my app and I want to add a possibility of sliding/swiping a cell on my tableview, exactly like in this answer: Swipe-able Table View Cell in iOS 9 but with a difference that it works only for one specific cell, not all of them. So far I have:
class MyTableViewController: UITableViewController {
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: (UITableView!), commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: (NSIndexPath!)) {
}
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
var deleteAction = UITableViewRowAction(style: .Default, title: "Hello There!") {action in
print("some action!!")
}
return [deleteAction]
}
}
and it adds the swiping for all cells and I want to add it only to the 3rd one on my list (cells are not generated dynamically). Is there a way of doing that?
You can check the indexPath of the cell which is given as parameter and return the appropriate response based on that. Furthermore, you could also check the type of your cell using func cellForRowAtIndexPath(_ indexPath: NSIndexPath) -> UITableViewCell? if you have different cells or so.
Index path is a class. You can get the index of the current row from the indexPath using indexPath.row.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
if ((indexPath.row % 3) == 0) {
return true
} else {
return false
}
}

editActionsForRowAtIndexPath is not working

I'm developing a small app using Swift, an UITableView to show custom cells, and then I would like to swipe to edit to do that I use the following code :
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! BasicCell
cell.Name.text = contacts[indexPath.row].Name
cell.Phone.text = contacts[indexPath.row].Phone
cell.ProfilPic.load(contacts[indexPath.row].ProfilPic!)
cell.ProfilPic.layer.masksToBounds = true
cell.ProfilPic.layer.cornerRadius = CGRectGetWidth(cell.ProfilPic.frame)/2.0
return cell
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?{
let delete = UITableViewRowAction(style: .Normal, title: "Delete") { action, index in
print("delete")
}
let done = UITableViewRowAction(style: .Default, title: "Done") { action, index in
print("done")
}
return [delete, done]
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// the cells you would like the actions to appear needs to be editable
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// you need to implement this method too or you can't swipe to display the actions
}
the problem is when I swipe a cell, buttons are not showing :

Swift: how to change Edit/Done buttons behaviour/function

In my iOS app I have a UITableViewController whose rows contain data and data picker. I want to change te function of the buttons Edit and Done. I would like the Edit button to allow the user to write/insert data (instead of deleting rows), while I would like the Done button to save to save the data (and not just exiting the edit mode). I've added to my code the following:
// The following two functions remove the red minus sign
override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.None
}
override func tableView(tableView: UITableView, shouldIndentWhileEditingRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return false
}
in order to avoid appearing the circle red minus, but how can I override the functions according to Edit or Done value of the button? I heard about delegates but I'm new to iOS and I don't know what they are...if someone can explain me I would be grateful.
Please try this code block :-)
import UIKit
// You can initialise an instance of this class manually or configure it on storyboard
class TableViewController: UITableViewController {
// MARK:- This part is for demo purpose
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.backgroundColor = UIColor.yellowColor()
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
// MARK:- From here is the main part to answer your question
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView, shouldIndentWhileEditingRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return false
}
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let editAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Edit") { (action, indexPath) -> Void in
print("Write/Insert data here")
}
let doneAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Done") { (action, indexPath) -> Void in
print("Save data here")
}
doneAction.backgroundColor = UIColor.blueColor()
return [doneAction, editAction]
}
}
You will see the result like this

Why do all Section Heading rows slide with table cell when i implement UITableViewRowAction

I have the following swift code to implement a UITableViewRowAction, when I swipe a row the row slides out to the left as expected, however all the Section Header Rows also slide to the left with the table row at the same time.
I have also included a screen shot to show what is happening
If I remove the viewForHeader override and replace it with titleForHeaderInSection then I have no problem.
The reason for overiding the viewForHeader is that I want to place an image in the Header Row.
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCellWithIdentifier("header") as UITableViewCell
cell.textLabel?.text = instrumentGroups[section].name
cell.imageView?.image = UIImage(named: instrumentGroups[section].name)
return cell
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> InstrumentTableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("instrumentCell", forIndexPath: indexPath) as InstrumentTableViewCell
cell.instrument = instrumentGroups[indexPath.section].instruments[indexPath.row]
return cell
}
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
let instrument = instrumentGroups[indexPath.section].instruments[indexPath.row]
var actions: [AnyObject] = []
var action = UITableViewRowAction(style: .Normal, title: "Remove Watch") { (action, indexPath) -> Void in
tableView.editing = false
instrument.SetWatchList(false)
self.refresh()
}
action.backgroundColor = UIColor.redColor()
actions.append(action)
return actions
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
}
Simply return cell.contentView instead of cell within your viewForHeaderInSection function and your problem will be resolved.
I had the same problem. The answer is simple.
Because you use UITableViewCell as header view. Try UILabel or UITableViewHeaderFooterView instead.

Resources