Uitableviewcelldeleteconfirmationcontrol Issue

UITableViewCellDeleteConfirmationControl issue

Here's my hackish unfinished implementation for people to build from.

This should work for iOS6-- and iOS7.

Obviously this code goes in your subclassed UITableViewCell.

-(void)willTransitionToState:(UITableViewCellStateMask)state{
NSLog(@"EventTableCell willTransitionToState");
[super willTransitionToState:state];
if((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask){
[self recurseAndReplaceSubViewIfDeleteConfirmationControl:self.subviews];
[self performSelector:@selector(recurseAndReplaceSubViewIfDeleteConfirmationControl:) withObject:self.subviews afterDelay:0];
}
}
-(void)recurseAndReplaceSubViewIfDeleteConfirmationControl:(NSArray*)subviews{
NSString *delete_button_name = @"button_panorama_no_arrow";
for (UIView *subview in subviews)
{
//handles ios6 and earlier
if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"])
{
//we'll add a view to cover the default control as the image used has a transparent BG
UIView *backgroundCoverDefaultControl = [[UIView alloc] initWithFrame:CGRectMake(0,0, 64, 33)];
[backgroundCoverDefaultControl setBackgroundColor:[UIColor whiteColor]];//assuming your view has a white BG
[[subview.subviews objectAtIndex:0] addSubview:backgroundCoverDefaultControl];
UIImage *deleteImage = [UIImage imageNamed:delete_button_name];
UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,deleteImage.size.width, deleteImage.size.height)];
[deleteBtn setImage:[UIImage imageNamed:delete_button_name]];
[[subview.subviews objectAtIndex:0] addSubview:deleteBtn];
}
//the rest handles ios7
if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationButton"])
{
UIButton *deleteButton = (UIButton *)subview;
[deleteButton setImage:[UIImage imageNamed:delete_button_name] forState:UIControlStateNormal];
[deleteButton setTitle:@"" forState:UIControlStateNormal];
[deleteButton setBackgroundColor:[UIColor clearColor]];
for(UIView* view in subview.subviews){
if([view isKindOfClass:[UILabel class]]){
[view removeFromSuperview];
}
}
}
if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"])
{
for(UIView* innerSubView in subview.subviews){
if(![innerSubView isKindOfClass:[UIButton class]]){
[innerSubView removeFromSuperview];
}
}
}
if([subview.subviews count]>0){
[self recurseAndReplaceSubViewIfDeleteConfirmationControl:subview.subviews];
}

}
}

I hope this helps someone.

UITableViewCell subviews returns only UITableViewCellScrollView in iOS7

You need to recursively descend into each subview's subviews and so on. Never make any assumptions about the private subview structure. Better yet, since you should only add subviews to the cell's contentView, just look in the contentView, not the whole cell.

Animating custom-drawn UITableViewCell when entering edit mode

Thanks to Craig's answer which pointed me in the right direction, I have a solution for this. I reverted my commit which moved the text position based on the editing mode and replaced it with a new solution that sets the entire content view to the correct position any time layoutSubviews is called, which results in an automatic animation when switching to and from edit mode:

- (void)layoutSubviews
{
CGRect b = [self bounds];
b.size.height -= 1; // leave room for the separator line
b.size.width += 30; // allow extra width to slide for editing
b.origin.x -= (self.editing) ? 0 : 30; // start 30px left unless editing
[contentView setFrame:b];
[super layoutSubviews];
}

By doing it this way I was able to remove the setFrame: override found in ABTableViewCell.m because its former logic plus my additions are now found in layoutSubviews.

I set a light grey background on the cells to verify a custom background works properly without allowing us to see behind it as it moves back and forth and it seems to work great.

Thanks again to Craig and anyone else who has looked into this.

GitHub commit for this solution: (link)

UITableView “swipe to delete” button frame issue

I'm not familiar with the animation code you're using, but I would try using willTransitionToState (and, if needed, didTransitionToState) instead of layoutSubviews to handle animation during editing tableViewCells.

Both have been available since iOS 3.0.

Place this code in your subclass of UITableViewCell. It will handle all of the transitions from one UITableViewCellStateMask to another, and you can implement the animation you need for the transition to each state. Just implement the animations you require in the proper place according to the NSLogs I added. (again, not familiar with your animation code, but I did test it and saw results using this code)

- (void)willTransitionToState:(UITableViewCellStateMask)state {

[super willTransitionToState:state];

if (state == UITableViewCellStateDefaultMask) {

NSLog(@"Default");
// When the cell returns to normal (not editing)
// Do something...

} else if ((state & UITableViewCellStateShowingEditControlMask) && (state & UITableViewCellStateShowingDeleteConfirmationMask)) {

NSLog(@"Edit Control + Delete Button");
// When the cell goes from Showing-the-Edit-Control (-) to Showing-the-Edit-Control (-) AND the Delete Button [Delete]
// !!! It's important to have this BEFORE just showing the Edit Control because the edit control applies to both cases.!!!
// Do something...

} else if (state & UITableViewCellStateShowingEditControlMask) {

NSLog(@"Edit Control Only");
// When the cell goes into edit mode and Shows-the-Edit-Control (-)
// Do something...

} else if (state == UITableViewCellStateShowingDeleteConfirmationMask) {

NSLog(@"Swipe to Delete [Delete] button only");
// When the user swipes a row to delete without using the edit button.
// Do something...
}
}

And if you need something to happen after one of these events, just implement the same code, but in didTransitionToState. The same UITableViewCellStateMasks apply.

UITableViewCell, Delete Button Frame?

If you are looking for a strongly true way to solve this problem then you should to subclass of UITableViewCell and override the all state handling methods for correct drawing your own delete button (do not call super in those methods). But there is another easy way:

@implementation CustomCell
- (void)layoutSubviews {
[super layoutSubviews];
if (self.showingDeleteConfirmation) {
if ([self.subviews count] < 4) return;
UIView *delBtn = [self.subviews objectAtIndex:3];
delBtn.frame = CGRectOffset(delBtn.frame, 0, 10);
}
}
@end

Change the color of default red color delete button in UITableViewCell when swiping rows or click on edit button


iOS 8 and 9 (props to this post)


Note: If you are working with an existing iOS 7 project, you'll need to update the target to iOS 8 to get this functionality. Also remember to set the UITableviewDelegate.

All the magic now happens here (as many buttons as you want too!!!!):

 -(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *button = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Button 1" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
NSLog(@"Action to perform with Button 1");
}];
button.backgroundColor = [UIColor greenColor]; //arbitrary color
UITableViewRowAction *button2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Button 2" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
NSLog(@"Action to perform with Button2!");
}];
button2.backgroundColor = [UIColor blueColor]; //arbitrary color

return @[button, button2];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// you need to implement this method too or nothing will work:

}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}


(iOS 7)


**activate the delete button on swipe**

// make sure you have the following methods in the uitableviewcontroller

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"You hit the delete button.");
}

set custom text label instead of delete.

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"Your Label";
}

set custom color for button part 1 - warning, this technically involves poking at the private apple API. However, you are not prevented from modifying a subview using a public method search that is part of UIKIT.

Create a uitableviewcell class (see also https://stackoverflow.com/a/22350817/1758337 )

- (void)layoutSubviews
{
[super layoutSubviews];
for (UIView *subview in self.subviews) {
//iterate through subviews until you find the right one...
for(UIView *subview2 in subview.subviews){
if ([NSStringFromClass([subview2 class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) {
//your color
((UIView*)[subview2.subviews firstObject]).backgroundColor=[UIColor blueColor];
}
}
}
}

Another note: there's no guarantee this approach will work in future updates. Also beware that mentioning or using the private UITableViewCellDeleteConfirmationView class may lead to AppStore rejection.

set custom color for button part 2

back in your uitableviewcontroller

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
[YourTableView reloadData];
}

(The alternate color won't be called until the next time layoutSubviews is called on the tablecell, so we ensure this happens by reloading everything.)



Related Topics



Leave a reply



Submit