UITableView disable swipe to delete, but still have delete in Edit mode?
Ok, it turns out to be quite easy. This is what I did to solve this:
Objective-C
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Detemine if it's in editing mode
if (self.tableView.editing)
{
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
}
Swift 2
override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
if tableView.editing {
return .Delete
}
return .None
}
Swift 3
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
if tableView.isEditing {
return .delete
}
return .none
}
You still need to implement tableView:commitEditingStyle:forRowAtIndexPath:
to commit the deletion.
Allow UITableView to reorder, but not delete in edit mode, and enable swipe to delete anyway
I believe the only thing you need to change in your code is the editingStyleForRowAtIndexPath
function. You only return .Delete
if the table view is not in editing mode.
This way swipe-to-delete still works (not in editing mode), and when you do switch to editing mode, the row can't be deleted.
Swift: How to delete with Edit button and deactivate swipe to delete?
Here's the working code:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
arr.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
if (self.tableView.editing) {
return UITableViewCellEditingStyle.Delete
}
return UITableViewCellEditingStyle.None
}
UITableView disable swipe to delete for particular cells swift
You might customize the UITableViewDelegate
's function editingStyleForRowAt
, especially returning UITableViewCellEditingStyle.none
when you don't need the swipe, something like:
public func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle
{
if mainList[indexPath.row].statusId == "12" {
return UITableViewCellEditingStyle.none
} else {
return UITableViewCellEditingStyle.delete
}
}
Disable Swipe-to-Delete Gesture in UITableView
Here's what to do:
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
// Detemine if it's in editing mode
if (self.tableView.editing) {
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
}
You still need tableView:commitEditingStyle:forRowAtIndexPath:
to animate the deletion.
This is a much cleaner solution than iBrad Apps' solution, since you can use the default self.editButtonItem
instead of a custom button.
Link: UITableView disable swipe to delete, but still have delete in Edit mode?
How do I disable the full swipe on a tableview cell in iOS11
Implement like below :
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let delete = UIContextualAction(style: .destructive, title: "Delete") { (action, sourceView, completionHandler) in
print("index path of delete: \(indexPath)")
completionHandler(true)
}
let swipeAction = UISwipeActionsConfiguration(actions: [delete])
swipeAction.performsFirstActionWithFullSwipe = false // This is the line which disables full swipe
return swipeAction
}
This is the line which disables full swipe
swipeAction.performsFirstActionWithFullSwipe = false
And remove the other functions if you implement any like editingStyle
and editActionsForRowAt
.
UITableView edit mode appears on top of my views
I suspect that when creating your UITableViewCell
subclass, you did not add your custom subviews to the cell's contentView.
From the documentation:
The content view of a UITableViewCell object is the default superview for content that the cell displays. If you want to customize cells by simply adding additional views, you should add them to the content view so they position appropriately as the cell transitions in to and out of editing mode.
Sounds like your problem exactly.
Related Topics
I Want to Create Aes 128 Using Cfb Encryption with No Padding in Objective C
Repeat Interval for Unnotification
Why My Nsdateformatter Returns Null
Firebase Querying - I Get Some Type of Nsarray as Result (Only Sometimes)
Retrieve User Defaults Information from an Apple Watch
iOS - Cydia Open Command and Its Counterpart
How to Analyze Stack Trace Info of a Thread
Save an Object in Nsuserdefaults and Realm
Transparent Uinavigationbar in Swift
Xcode 8: Preparing Archive Takes Forever
Code Sign Error on Xcode 8 and iOS 10 Cordova Project
How to Extract a Url from a Sentence That Is in a Nsstring
How to Know If an Emoji Is Supported in iOS
Facebook Logout Is Not Working in New Sdk V.4.1.0 in iOS
Adjust Font Size of Text to Fit in Uibutton
How to Get Weekday And/Or Name of Month from a Nsdate Variable
"File Not Found", "Linker Command Failed with Exit Code 1" in Xcode 4.5.1