I'd like to add a custom button on my cells that do the same thing as swipe-to-delete function. So when clicking on my custom button, this one will be hide to let appear the official red "Delete" button.
So I did something like that :
/// Controller.m
///
/// @brief Delete icon button pressed. Trigger display of Delete full button
///
- (IBAction)deleteDrug:(id)sender event:(id)event {
NSIndexPath *indexPath = [self indexPathForButton:sender event:event];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[cell setEditing:YES animated:YES];
}
/// CustomCell.m
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
// hide / show "modify" button when entering in edit mode
switch (editing) {
case YES:
self.deleteButton.hidden = YES;
break;
case NO:
self.deleteButton.hidden = NO;
break;
default:
break;
}
}
At this moment, my custom button are getting hide when clicking on them but the official red "Delete" button is not appearing.
Do someone know how to handle this ?
I believe the delete button is handled more by the tableView. So rather than setting your cell editing you may need to let the tableView know it's supposed to be editing.
- (IBAction)deleteDrug:(id)sender event:(id)event {
selectedButtonIndex = [self indexPathForButton:sender event:event];
[tableView setEditing:YES animated:YES];
}
So you may need to do something like set the tableView to edit. Then in your tableview's datasource you can implement this method, where selectedButton is the index path for the proper cell.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath == selectedButtonIndex) {
return YES;
}
return NO;
}
You may need to implement this method for your datasource.
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
I didn't find any appropriate solution to this question, so I made my own. I made fake delete button and it works pretty the much like the original one. I show the tricky delete button part here.
If you want to know how to replace default minus (delete) edit control, there is nice article about it, I recommend: http://vinsol.com/blog/2015/01/06/custom-edit-control-for-uitableviewcell/
So, assume you made first part from the article and you want to see delete button when your custom edit control button are pressed. The code is on the Swift, but you can use same steps to make this in Objective-C
func deleteButtonShow(){
let deleteButton = UIButton(frame: CGRect(x: self.frame.width, y: 0, width: 80, height: self.frame.height))
deleteButton.backgroundColor = UIColor.redColor() // ! note: original delete have another color
deleteButton.setTitle("Delete", forState: UIControlState.Normal)
deleteButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
deleteButton.addTarget(self, action: "actionForDelete", forControlEvents: UIControlEvents.TouchUpInside)
// action for your button will call delegate that will do the delete work. in most cases
self.contentView.addSubview(deleteButton)
var movedSubviews = self.subviews
var i = 0
for subview in movedSubviews {
// NOTE: I added tag = 1 in the storyboard to Content View of my cell
if subview.tag == 1 {
movedSubviews.removeAtIndex(i) // we don't need to move it.
break
}
++i
}
movedSubviews.extend(self.contentView.subviews)// add subviews from content view
UIView.animateWithDuration(0.3, animations: { () -> Void in
for movedView in movedSubviews as! [UIView] {
movedView.frame = CGRect(origin: CGPoint(x: movedView.frame.origin.x - 80, y: movedView.frame.origin.y), size: movedView.frame.size)
//animate the things
}
})
}
We don't want to move content view as I found that if our button is not on the above the content view it is untouchable, I have some thoughts why, but not sure. If you know why, please add this info in the comments, I would love to know it.
I also want to note, that if you use autolayout as me, you need to change constraints in the animation, not it's frame, so you might no need extend
part.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With