UITableViewCell delete button gets covered up

2019-01-08 19:35发布

UPDATE:

Thanks to information from "Evgeny S" I've been able to determine that what is covering up the delete button is the cell background. I had the following function called at the beginning of cell init:

- (void) setupGradientsWithHeight:(int) the_height
{
    CAGradientLayer* gradient = [CAGradientLayer layer];

    gradient.frame  = CGRectMake(self.bounds.origin.x,
                                 self.bounds.origin.y,
                                 self.bounds.size.width,
                                 the_height);

    gradient.colors = [NSArray arrayWithObjects:
                       (id)[UIColor colorWithRed:218.0f/255.0f
                                           green:218.0f/255.0f
                                            blue:220.0f/255.0f
                                           alpha:1.0].CGColor,
                       (id)[UIColor colorWithRed:180.0f/255.0f
                                           green:180.0f/255.0f
                                            blue:184.0f/255.0f
                                           alpha:1.0].CGColor,nil];

    UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];

    [bgview.layer addSublayer:gradient];
    [self setBackgroundView:bgview];
}

The problem is solved by NOT adding the backgroundview. However, I'm wondering how it might be solved while preserving the gradient background for each cell?

One of two things could be happening. When the delete button appears the background view is either being shifted and then back - or its size is being decreased and then back. I tried messing with the width component "self.bounds.size.width" for the gradient frame but I still don't see how that could be affecting things because this function is only called once at cell initialization.

ORIGINAL POST:

I had created an App in Xcode4 under iOS6 SDK - but after loading things up in the new environment (Xcode5 and iOS7 SDK) I ran into a problem. Note that I have resolved all build errors and warnings that came up when making the transition.

I have a tableview with some cells. After entering edit mode, if one clicks the red toggle button to initiate delete mode for a cell - the animation begins and the "Delete" button appears - but only briefly and is then suddenly re-hidden.

Another strange thing I should note is that even while the delete button is covered up - you can still click where it should be and delete the cell.

I've tried to take out as much of my "custom code" as possible, I've removed functions that I had overridden and tried to take things back to an original state as possible - but to no avail.

At one point I had seen a strange message in the output log: "attempting to set a swipe to delete cell when we already have one....that doesn't seem good". For some reason the message is on longer appearing and I can't seem to remember what I was doing at the time.

Any thoughts or recommendations on what might be causing this?

http://www.youtube.com/watch?v=vCAGzSaOwp4

12条回答
我命由我不由天
2楼-- · 2019-01-08 19:48

here is a better way just make sure you subclass UITableviewController for better performance and add this code

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {

[super setEditing:editing animated:animated];

CGRect frame = self.tableView.frame;

if(editing){
    [self.tableView setFrame:CGRectMake(0, frame.origin.y, frame.size.width, frame.size.height)];
    //other codes to run if needed
}else{
    [self.tableView setFrame:frame];
    //other codes to run if needed
}

}

查看更多
smile是对你的礼貌
3楼-- · 2019-01-08 19:50

You can avoid the problem by setting background you want for the uitableview and setting cell.backgroudColor = [UIColor clearColor]; and cell.contentView.backgroundColor = [UIColor clearColor];

查看更多
做自己的国王
4楼-- · 2019-01-08 19:52

I fixed this by finding the delete button view and bringing it to the front. I did this in layoutSubviews in a UITableViewCell subclass.

Here's a small bit of code that should give you an idea of how to do it:

- (void)layoutSubviews
{
    [super layoutSubviews];

    for (UIView *subview in self.subviews) {

        for (UIView *subview2 in subview.subviews) {

            if ([NSStringFromClass([subview2 class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) { // move delete confirmation view

            [subview bringSubviewToFront:subview2];

        }
    }
}
查看更多
▲ chillily
5楼-- · 2019-01-08 19:52

I've run into a similar problem and have a work around the that puts the Delete button on top of my cell content but the animation is a bit ugly. When done the button is visible and does the right thing.

Create a custom subclass of UITableViewCell and add the following code:

-(void)didTransitionToState:(UITableViewCellStateMask)state
{
    if (state & UITableViewCellStateShowingDeleteConfirmationMask ) {
        [self sendSubviewToBack:self.contentView];

    }

    [super didTransitionToState:state];
}

Improvements on this approach or other pointers would be appreciated.

查看更多
Lonely孤独者°
6楼-- · 2019-01-08 19:57

Cleaner, General Solution

Assuming an iOS7 only app, with a technique similar to that posted by chris above, I believe the approach here: https://stackoverflow.com/a/19416870/535054 is going to be cleaner.

In this approach, you don't need to subclass your backgroundView, which could be different for different cells.

Place the code in the answer I've linked to above, in the root of your custom table cell hierarchy, and all of your table cells (that inherit from it), get the fix whenever they use the backgroundView or selectedBackgroundView properties.

查看更多
7楼-- · 2019-01-08 19:58

I was facing an issue where my UITableView was in editing mode and after deleting 2-3 rows I get message "attempting to set a swipe to delete cell when we already have one....that doesn't seem good" logged and then user Interaction on all cells except one cell gets disabled.

I solved this issue by

[myTableView setEditing:NO animated:NO];
[myTableView setEditing:YES animated:NO];

after each deletion.

This workaround worked in my case.

查看更多
登录 后发表回答