I know there is the same question for Objective-C and it has no answer, but maybe somebody has an idea.
I have an UIButton in a UITableView cell:
let tButton = UIButton(type: UIButtonType.Custom)
tButton.addTarget(self, action: "buttonTap:", forControlEvents: .TouchUpInside)
cell.contentView.addSubview(tButton)
And my commitEditingStyle delegate looks this way:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
}
When I start a swipe at the button, the buttons target gets fired.
Ok, ended up with:
func tableView(tableView: UITableView, willBeginEditingRowAtIndexPath indexPath: NSIndexPath) {
blocked = true
}
func tableView(tableView: UITableView, didEndEditingRowAtIndexPath indexPath: NSIndexPath) {
blocked = false
}
And then determine uiStatus in the targets of my buttons like so:
func buttonTap(sender:UIButton!){
if blocked == false {
...do something
}
}
Not very elegant, but it works. Good Night
Related
I need pan gesture like editingStyle in Swift, but I need this gesture for whole row and on end of gesture do action, which delete the cell.
I don't know, how to do it, because I am beginner. I know, that I must use UIGestureRecognizer, but I don't know how to do with that. I found some options, but all of that was only for delete without animation and I need animation and background for swiping.
I have only this code for show dynamic data:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TasksCell", for: indexPath) as! TasksCell
let arrayTasks = tasks[indexPath.row]
cell.taskName.text = arrayTasks.content
return cell
}
Can you help me with that?
You can use UITableView Delegate method to delete row. No need to use Pan Gesture. Please try this
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
{
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
if editingStyle == .delete
{
yourArray.remove(at: indexPath.row)
yourTableView.reloadData()
}
}
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
}
}
I have custom UITableViewCell with some content on it. While I am trying to swipe (to show delete custom button with image) It's simply not working, or if I'll try to do that 10 times, so I could see the swipe delete button action.
my code is:
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
//
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! MessagesCell
let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Destructive, title: " ") { value in
// here some removing action
deleteAction.backgroundColor = UIColor(patternImage: UIImage(named: "swipeToDelete")!)
return [deleteAction]
}
This code will help
func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
return true
}
func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
// handle delete (by removing the data from your array and updating the tableview)
}
}
Thank you.
The issue was in IIViewDeckController that blocked swipe with its PanGesture
I am trying to delete a row in table view. I have implemented the required methods but when i am swiping the row horizontally no delete button is coming. I have searched and I have found the same solution everywhere but it is not working in my case. I don't know where i am making mistake. Can anyone help me please?
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool
{
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
if editingStyle == .Delete
{
dataHandler.deletePeripheral(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
}
If below coding is helpful for you,i will be happy
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool
{
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
if editingStyle == .Delete
{
yourArray.removeAtIndex(indexPath.row)
self.tableView.reloadData()
}
}
SWIFT 3.0
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
{
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
if editingStyle == .delete
{
yourArray.remove(at: indexPath.row)
tblDltRow.reloadData()
}
}
You have to refresh the table.
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
self.tableView.beginUpdates()
self.arrayData.removeObjectAtIndex(indexPath.row) // also remove an array object if exists.
self.tableView.deleteRowsAtIndexPaths(NSArray(object: NSIndexPath(forRow: indexPath.row, inSection: 2)), withRowAnimation: UITableViewRowAnimation.Left)
self.tableView.endUpdates()
}
Apply Autolayout Constraints to table view. The delete button is there but not displaying because of no auto layout
I struggled with this as well but finally found out a solution - I am running XCode 8.3.2. A lot of the answers I read were for older versions of XCode / Swift and many were for Objective-C.
The solution I found was to use the 'editActionsForRowAt' tool for UITableViews. Note: Everything else I read kept pointing me towards the 'commit editingStyle' tool for UITableView, but I could never get it to work. Using editActionsForRowAt worked perfectly for me.
NOTE: 'postData' is the name of the Array that I added data to.
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteAction = UITableViewRowAction(style: .default, title: "Delete", handler: { (action, IndexPath) in
// Remove item from the array
postData.remove(at: IndexPath.row)
// Delete the row from the table view
tableView.deleteRows(at: [IndexPath as IndexPath], with: .fade)
})
// set DeleteAction Button to RED (this line is optional - it simply allows you to change the color of the Delete button - remove it if you wish)
deleteAction.backgroundColor = UIColor.red
return [deleteAction]
}
I also have some code to add an 'EDIT button' alongside the Delete button:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let editAction = UITableViewRowAction(style: .default, title: "Edit", handler: { (action, IndexPath) in
//print("Edit tapped")
})
// Set EditAction button to blue (this line is not mandatory - it just sets the color of the Edit box to blue)
editAction.backgroundColor = UIColor.blue
let deleteAction = UITableViewRowAction(style: .default, title: "Delete", handler: { (action, IndexPath) in
//print("Delete tapped")
// Remove item from the array
postData.remove(at: IndexPath.row)
// Delete the row from the table view
tableView.deleteRows(at: [IndexPath as IndexPath], with: .fade)
})
// set DeleteAction Button to RED (this line isn't mandatory - it just sets the color of the Delete box)
deleteAction.backgroundColor = UIColor.green
return [editAction, deleteAction]
}
EDIT: fixed format so code showed up in the grey box :)
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
if editingStyle == UITableViewCellEditingStyle.Delete {
numbers.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
for swift 3
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
dataHandler.deletePeripheral(indexPath.row)
tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
// - OR -
// mTableView.reloadData() //mTableView is an outlet for the tableView
}
}
i know what prob you are having . I think you just check your table constraints they may be wrong just recheck your auto layout constraints
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
yourArray.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool
{
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
if editingStyle == .Delete
{
carsArray.removeAtIndex(indexPath.row)
self.tableView.reloadData()
}
}
I have a piece of code here, which seems to be working just fine apart from one little piece.
import UIKit
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var tblTasks: UITableView!
//Returning to view
override func viewWillAppear(animated: Bool) {
tblTasks.reloadData();
}
//UItableViewDataSource
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return taskMgr.tasks.count
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
// handle delete (by removing the data from your array and updating the tableview)
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "test")
cell.textLabel?.text = taskMgr.tasks[indexPath.row].name
cell.detailTextLabel?.text = taskMgr.tasks[indexPath.row].desc
return cell
}
}
I have introduced these lines in order to be able to delete rows in the table view
func tableView(tableView: UITableView,
canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView,
commitEditingStyle editingStyle: UITableViewCellEditingStyle,
forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
// handle delete (by removing the data from your array and updating the tableview)
}
}
and what they do is giving me the possibility to swipe rows from right to left, though the row doesn't get deleted.
What would be the best possible way to introduce a DELETE button in this case?
best regards, Vlad
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
//delete row at selected index
numbers.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
Here is a cleaned up version for Swift 3.x:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete from your source, then update the tableView
numbers.removeAtIndex(indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
}
}
Check to make sure that the editingStyle is delete before proceeding
Remove the value from your data source (in this case, numbers)
Finally, pass in the indexPath as an array to be deleted, along with the animation you would like (in this case, automatic)
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
yourArray.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}