UITableView swipe delete bottom line remove - ios

I want to remove bottom line. See below image
I have simple code for swipe delete and UIView in uitableview
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == .delete) {
// handle delete (by removing the data from your array and updating the tableview)
}
}

Related

UITableView not showing Delete button on clicking minus button(Only on iOS 13)

I have a table view to shows a list of items. I have implemented the default editing style delete. It's been working till iOS 12. But not on iOS 13. It does not show the Delete button when clicking the minus button.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return machineOrders.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MachineOrderCell") as! MachineOrderCell
let machineOrder = machineOrders[indexPath.row]
cell.update(with: machineOrder)
cell.decreaseQuantityButton.isEnabled = canDecreaseQuantity(for: machineOrder)
cell.onDecreaseQuantity = {
self.delegate.view(self, didSelectDecreaseQuantity: machineOrder)
}
cell.onIncreaseQuantity = {
self.delegate.view(self, didSelectIncreaseQuantity: machineOrder)
}
return cell
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
navigationItem.rightBarButtonItem = doneBarButton
}
func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) {
navigationItem.rightBarButtonItem = !isCartEmpty ? (tableView.isEditing ? doneBarButton : editBarButton) : nil
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
let machineOrder = machineOrders[indexPath.row]
if editingStyle == .delete {
self.delegate.view(self, didSelectDelete: machineOrder)
}
}
Press minus button for some seconds, I mean long press, then it will work.

UITableViewCell row action while table is editing

I have a UITableView which I need to both reorder and allow for cells to be deleted by swiping left on a cell to reveal the Delete action. Is it possible to have both at the same time?
For the table I have:
tableView.allowsSelectionDuringEditing = true
tableView.isEditing = true
For the datasource:
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
// I do nothing here
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
// This is where I rearrange my data source array
}
And for the delegate:
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return .none
}
func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
return false
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .destructive, title: "Remove") { action, index in
// delete row
}
delete.backgroundColor = UIColor.appRed
return [delete]
}
So what I want to have is:
Reorder control allowing me to move a cell
Swipe left to reveal cell actions
And I need these to be visible and work at the same time. Can this be done?
Swipe to delete is not possible when your UITableView is in editing mode.
If you want to hide the delete button on the left when in editing mode you have to implement the following methods:
override func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
return false
}
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return tableView.isEditing ? .none : .delete
}
I think this is the closest you can get.

Swipe Delete in UItableviewCell

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

UITableView Force Right-to-Left swipe to delete not work

I have uiviewcontroller with uitableview
and swipe to delete work fine but
when I set uitableview semantic to force right-to-left the swipe to delete not even shown in the tableview
classViewController:UIViewController,UITableViewDelegate,UITableViewDataSource {
var yourArray : [Type] = []
override func viewDidLoad() {
super.viewDidLoad()
tablView.delegate = self
tablView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return yourArray.count
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
tableView.beginUpdates()
yourArray.remove(at: indexPath.row)
tableView.endUpdates()
tableView.reloadData()
}
}
}

Delete button in UITableView (SWIFT)

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

Resources