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)
}
}
Related
I have got to the point where I can add cells that all have different labels. But, when the row is swiped to be deleted it will only delete the text associated with the cell and not the cell itself. I've tried to add SavedMessages.deleteRows(at: [indexPath], with: .automatic) inside the if statement but every time the app tries to delete the row, the app crashes.
Here is the code:-
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
if editingStyle == UITableViewCellEditingStyle.delete {
let delete: NSFetchRequest<Messages> = Messages.fetchRequest()
do {
var deleteMessage = try
PersistenceServce.context.fetch(delete)
PersistenceServce.context.delete(messages.remove(at: indexPath.row))
PersistenceServce.saveContext()
self.messages = deleteMessage
SavedMessages.reloadData()
} catch {}
}
}
Thanks in advance!
Maybe you are not using
tableView.cellForRow(at: indexPath)?.removeFromSuperview()
I created an example where removing data as well as the cell is getting removed perfectly fine.
var data = [1,2,3,4,5]
override func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int
{
return data.count
}
override func tableView(_ tableView: UITableView,
commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete
{
data.remove(at: indexPath.row) //removing the value from data source.
tableView.cellForRow(at: indexPath)?.removeFromSuperview() //removing the cell
tableView.reloadData() //reloading Table data.
}
}
in my iPad app swipe deleting not working properly. after swipe deleting it shows the delete button on cell and this not disappearing.
To delete row from UITableView implement UITableViewDataSource method:
Swift
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
// enter your datasource updation logic hare & delete table cell
tableview.deleteRows(at: [indexPath], with: .fade)
}
}
have you write canEditRowAt method?
func tableView(_ tableView: UITableView, canEditRowAt indexPath:
IndexPath) -> Bool {
// Return YES if you want the specified item to be editable.
return true
}
and then actual code to delete in this function
func tableView(_ tableView: UITableView, commit editingStyle:
UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
//add code here for delete action
}
}
I need to delete a row in a section but it deletes the row in upper section. Please help find problems of my codes. Also, please advise how to delete the section when deleting the row? There is one row in a section.
var sectionTitles = [String]()
var savedURLs = [[String]]()
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return savedURLs.count
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionTitles[section]
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return savedURLs[section].count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("SavingCell")
cell?.textLabel!.text = savedURLs[indexPath.section][indexPath.row]
return cell!
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
savedURLs[indexPath.section].removeAtIndex(indexPath.row)
NSUserDefaults.standardUserDefaults().setObject(savedURLs, forKey: Key_SavedURLs)
savingTableView.reloadData()
}
}
To delete empty sections modify your code to this:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
savedURLs[indexPath.section].removeAtIndex(indexPath.row)
// now check if a section is empty and, if so, delete it
if savedURLs[indexPath.section].count == 0 {
savedURLs.removeAtIndex(indexPath.section)
}
NSUserDefaults.standardUserDefaults().setObject(savedURLs, forKey: Key_SavedURLs)
savingTableView.reloadData()
}
}
although for this to work your savedURLs should be [[String]]
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
savedURLs.removeAtIndex(indexPath.section)
NSUserDefaults.standardUserDefaults().setObject(savedURLs, forKey: Key_SavedURLs)
savingTableView.reloadData()
}
}
try this.this should work..
I have UIViewController and I dragged a UITableView inside and I want to insert some elements to the table and be able to remove them here . When I use this code it works fine for UITableViewController but now when I want to remove a cell I can swap it but the deleting animation (Red sign) doesn't show up
import UIKit
class ViewController: UIViewController , UITableViewDataSource , UITableViewDelegate {
var listArray = ["Danial"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBOutlet var table: UITableView!
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell()
cell.textLabel?.text = listArray[indexPath.row]
return cell
}
func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool
{
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete){
listArray.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) }
}
}
The problem with your code is that you are typing:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete){
listArray.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) }
}
instead of
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete){
listArray.removeAtIndex(indexPath.row)
self.table.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) }
}
so just go to everywhere where you wrote
tableView.
and change it to self.table.
(basically what you named your tableview from the IBOutlet)
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()
}
}