2

I have a UITableViewCell with the following hierarchy:

enter image description here

I use swipe and pan gestures to move the Container View to left, so that the trash button is visible, Similar to Instagram:

enter image description here

But, on cell selection The thrash button is visible:

enter image description here

EDIT:

Please Note:

The trash button is actually a custom button with a transparent png image. The background colour of the button is then set as red.

Please provide your valuable suggestions or a solution to this problem. Thanks!

Community
  • 1
  • 1
GoGreen
  • 2,251
  • 1
  • 17
  • 29
  • Change the color which should not be similar to white and check – Hussain Shabbir Dec 10 '14 at 07:27
  • write custom UITableViewCell, override - (void)setSelected:(BOOL)selected animated:(BOOL)animated and - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated ==> set Trash buttom hidden = YES when highlighted or selected; OR maybe you should read this link: http://stackoverflow.com/questions/17254402/swipe-to-delete-and-the-more-button-like-in-mail-app-on-ios-7 – hahv Dec 10 '14 at 07:31
  • @HussainShabbir change the color of what? I didnt get you. – GoGreen Dec 10 '14 at 07:38
  • What's the question? How to hide the button when selected? How to make the button red when selected? – Matthias Bauch Dec 10 '14 at 07:39
  • The question isn't about hiding anything. From the view hierarchy, you may notice that the trash button lies beneath the container view. Then why does it show on cell selection? My expectation is that it should not show. – GoGreen Dec 10 '14 at 07:41
  • Please see the edit above. – GoGreen Dec 10 '14 at 07:43
  • @HoangVanHa that seems to be an ugly work around. But I will try that if no better solution comes up. Thanks for the pointer! – GoGreen Dec 10 '14 at 07:44
  • 2
    downVoter: Please provide a valuable suggestion or a solution to the problem. If you don't understand the question, please ask. Downvoting a question blankly doesn't help anyone. Thanks! – GoGreen Dec 10 '14 at 08:27

1 Answers1

0

As I didn't get a proper answer, I was forced to fix this issue as pointed out by @hoang Van Ha.

In the custom cell class:

I hid the button by overriding setSelected method as follows:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    if (self.cellContainerView.frame.origin.x == 0) {
        self.trashButton.hidden = YES;
    }
}

at the beginning of the gesture methods, i.e. pan and swipe I show the button again and on completing the gesture, I check if the frame of the container view has origin zero. If so, the button should be hidden.

- (IBAction)handleSwipeGesture:(UISwipeGestureRecognizer *)recognizer
{
    self.trashButton.hidden = NO;
    if ( recognizer.direction == UISwipeGestureRecognizerDirectionRight ) {

        [UIView animateWithDuration:0.2
                     animations:^{
                         self.cellContainerView.frame = CGRectMake(0, self.cellContainerView.frame.origin.y, 320, self.cellContainerView.bounds.size.height);
                     }
                     completion:^(BOOL finished) {
                         self.trashButton.hidden = YES;
                     }
         ];
    }
}

In the controller class, in cell for row method, do the same process.

 if (cell.cellContainerView.frame.origin.x == 0) {
         cell.trashButton.hidden = YES;
 }

Please feel free to comment, or even post a better answer!

GoGreen
  • 2,251
  • 1
  • 17
  • 29
  • 1
    Here is why the image gets visible: When a cell gets highlighted the UITableView goes over all the subViews of the cell and sets their backgroundColor to clear. – Matthias Bauch Dec 15 '14 at 12:06
  • I thought so too. Can you please point me to a blog or a documentation of the same? – GoGreen Dec 15 '14 at 13:28
  • 1
    I think this is not documented. I found that a while ago when I tried to prevent this (quite annoying) behavior. If you need to see it yourself you can subclass any UIView and put it in a cell. If you overwrite `setBackgroundColor:` and put a NSLog (or a breakpoint) in you can see how highlight changes the backgroundColor to clear and unhighlight changes it back. – Matthias Bauch Dec 15 '14 at 13:59
  • Yep I tried that out. That is exactly whats happening. Thanks @MatthiasBauch :)! If you post that as an answer, I will surely accept it. – GoGreen Dec 16 '14 at 07:43