Is there any way to hide - (Delete) button while editing UITableView
Here is my complete solution, without indentation (0left align) of the cell!
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone;
}
- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
- (BOOL)tableView:(UITableView *)tableview canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
Hide Delete button from UITableViewCell
To get the actual animation (Instead of the UITableViewRowAnimationRight
/UITableViewRowAnimationAutomatic
) animations, just do
[self.tableView beginUpdates];
[self.tableView setEditing:NO animated:NO];
[self.tableView setEditing:YES animated:NO];
[self.tableView endUpdates];
beginUpdates
and endUpdates
provide the animation, and the tableView is just switched from not editing to editing instantly, which closes the delete button.
Hope this helps!
Swipe to delete works, but the delete button doesn't
ok, the problem is the tapgesturerecognizer in your navigationcontroller. if you want to keep it you could use something like the following:
class NavigationController : UINavigationController, UIGestureRecognizerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(NavigationController.dismissKeyboard))
tap.delegate = self
view.addGestureRecognizer(tap)
}
func dismissKeyboard() {
view.endEditing(true)
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
if let view = touch.view where String(view.dynamicType) == "UITableViewCellEditControl" {
print("do not receive touch")
return false
}
print("do receive touch")
return true
}
}
Swift 4 - UITableViewController hide delete button editingStyleForRowAt .delete
Try using this instead of those three functions you tried:
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteAction = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
self.array.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
}
return [deleteAction]
}
EDIT:
After seeing your screenshot, I believe this is what you are trying to achieve. You are trying to enable deleting when an editButton
is pressed, and then the delete icon appears. If so, please try this code.
class TableViewController: UITableViewController {
var array = [1,2,3]
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
navigationItem.rightBarButtonItem = editButtonItem
}
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
tableView.setEditing(editing, animated: true)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "\(array[indexPath.row])"
return cell
}
override func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
return false
}
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .delete
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
//deleting logic
}
}
Otherwise, you may consider implementing just a slide to delete animation, and the code is that before the EDIT.
UITableView disable delete confirmation
I came up with one solution, but it risks rejection. (Does checking for an undocumented class by classname constitute a sdk-agreement violation?)
Perhaps someone can improve on this?
Basically, in the context of my custom UITableViewCell, I watch for the delete button (not a UIButton, unfortunately) to be added, then invoke it's UIControlEventTouchUpInside action. With a bit of delay so the animations work out:
- (void) addSubview:(UIView *)view
{
if ([NSStringFromClass([view class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"])
{
UIControl* c = (UIControl*)view;
NSArray* actions = [c actionsForTarget: self forControlEvent: UIControlEventTouchUpInside];
[self performSelector: NSSelectorFromString( [actions lastObject] ) withObject: view afterDelay: .25];
return;
}
[super addSubview: view];
}
UITableView edit mode delete button is hiding in IPhone 4s
OK, so from your comment your table view is not constrained to the view properly. So your cells are "hanging" off the right edge of the screen.
To fix this you should add constraints in your storyboard.
Select the table view and hit the add constraints button. Select the four edges and put 0 in the box.
Then hit "add constraints" this should fix the problem you are having.
The add constraints button is the one that looks like a Star Wars Tie Fighter in the bottom right corner of the Interface Builder screen...
Related Topics
How to Tap on a Specific Point Using Xcode Uitests
Deploying an iOS Application Using Apple Enterprise Developer Program
Swift: Long Press Gesture Recognizer - Detect Taps and Long Press
iOS 14 How to Trigger Local Network Dialog and Check User Answer
How to Set Aspect Ratio Constraints Programmatically in iOS
Fbsdkloginmanager Loginwithpublishpermissions Always Returns Iscancelled=Yes
How to Convert Bytes to a Float Value in Swift
iOS 8 Uiactivityviewcontroller and Uialertcontroller Button Text Color Uses Window's Tintcolor
Error: Uicollectionview Must Be Initialized with a Non-Nil Layout Parameter
Multiline Label in Uistackview
How to Add Older Version of iOS Sdk in Xcode 4.5
Xcode 5.1: Libcordova.A Architecture Problems
Could Not Launch Process Launch Failed: Timed Out Waiting for App to Launch
Modify Uiimage Renderingmode from a Storyboard/Xib File
How to Make Uiimagepickercontroller for Camera and Photo Library at the Same Time in Swift
Blocks on Swift (Animatewithduration:Animations:Completion:)