0

I have a UITableView with custom UITableViewCells. The content of the cell is kept in a separate class extending UIView (i have 3 kind of contents which i switch by hiding and showing views, i took this approach because of the way i wanted to make the transition between cells). So let's assume i have one of the views visible and added to the contentView of the cell. This view contains some text and some rectangles made of UIViews. Everything good till now, but the weird thing is when i touch the cell, the rectangles simply disappears, the text stays the same. Do you know what might be the problem? I tried to add the view to the backgroundView as well, it's doing the same.

- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

if (self) {
    self.active = NO;
    state = -1;

    // Touch is not working if the view has no bounds
    self.backgroundView = [[UIView alloc] init];
    self.backgroundColor = [UIColor clearColor];
    self.selectedBackgroundView = [[UIView alloc] init];
    self.selectedBackgroundView.backgroundColor = [UIColor clearColor];
    self.clipsToBounds = YES;
    self.contentView.clipsToBounds = YES;

    // Add empty view
    emptyView = [[View1 alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) andRadius:CELL_DOT_RADIUS];
    emptyView.userInteractionEnabled = NO;
    [self.backgroundView addSubview:emptyView];

The view:

- (id) initWithFrame:(CGRect)frame andRadius:(int)r {
self = [super initWithFrame:frame]; radius = r;

if (self) {

    int outerlineWeight = 1;

    timelineView = [[UIView alloc] initWithFrame:CGRectMake(frame.size.width/4, frame.size.height/2, 1, 1)];
    [self addSubview:timelineView];


    dotView = [[UIView alloc] initWithFrame:CGRectMake(frame.size.width/4-radius, frame.size.height/2-radius, radius*2, radius*2)];
    dotView.layer.cornerRadius = radius;
    dotView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    [self addSubview:dotView];

    UIView *n = [[UIView alloc] initWithFrame:CGRectMake(160, 0, 50, 20)];
    n.backgroundColor = [UIColor redColor];
    [self addSubview:n];


    middleText = [[UILabel alloc] initWithFrame:dotView.frame];
    middleText.font = [UIFont systemFontOfSize:12];
    middleText.textColor = [UIColor grayColor];
    middleText.backgroundColor = [UIColor clearColor];
    middleText.textAlignment = NSTextAlignmentCenter;
    [self addSubview:middleText];

This are the sources so you can try for yourself if you want. As you can see the orange rectangle disappears but the text not. For what i need to do nothing should disappear, in the best case i want to change their colours. http://ge.tt/6wRBObD1/v/0?c

Cristi Băluță
  • 1,295
  • 15
  • 27
  • 2
    You haven't added it to the content view. You've added it to the background view, which isn't shown when you select the cell. – jrturton Jan 10 '14 at 08:35
  • Hi, i added the sources if you want to try, it doesn't matter where i add my view as everything is transparent. – Cristi Băluță Jan 10 '14 at 10:00
  • Transparent is irrelevant. When you select the cell, the backgroundView is **hidden**, not sent to the back, so anything you've added to it disappears. Add your view to the content view. – jrturton Jan 10 '14 at 10:13
  • And this is what i'm saying that not everything disappears, the text does not disappear. However the backgroundView never disappears according to Apple about selectedBackgroundView: "UITableViewCell adds the value of this property as a subview only when the cell is selected. It adds the selected background view as a subview directly above the background view (backgroundView) if it is not nil, or behind all other views. Calling setSelected:animated: causes the selected background view to animate in and out with an alpha fade." – Cristi Băluță Jan 10 '14 at 10:41
  • You're right about that, sorry. I still think you should be adding it to the content view, though. – jrturton Jan 10 '14 at 11:56
  • I think the backgroundColor is the problem. If i add a backgroundColor to my UILabel, when i select the cell the color disappears but the text not. If i add a border to my rectangle the background color disappears but the border not. Probably they want to make sure you see the background selected state of the cell. – Cristi Băluță Jan 10 '14 at 14:13

5 Answers5

0

You are adding it to background view. When selected the selected background view will display not background view. In your custom cell try

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    if (selected) {
        [self.backgroundView addSubview:emptyView];
    }
    else{
        [self.selectedBackgroundView addSubview:emptyView];
    }
    // Configure the view for the selected state
}

Or add empty view to contentview

Johnykutty
  • 12,091
  • 13
  • 59
  • 100
  • Thanks, this is kind of working, when i touch the cell everything is like it should be, but the unselected cells are completely blank. If i start to play with the parent views i get from where i started. I added the sources if you want to play. – Cristi Băluță Jan 10 '14 at 10:03
  • 1
    Or add empty view to contentview it may be better way – Johnykutty Jan 10 '14 at 10:05
0

one thing for frame settings u need to override "layoutsubviews" method ... in custom cell

  • init all the views in initialisation method i,e ur - (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier method and add it to cell

  • in - (void)layoutSubviews just set the frame for ur views in the cell,

use tag property to access the views in the layoutsubviews, if it is not the property


    - (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

      self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

      if (self) {
          self.active = NO;
          state = -1;

       // Touch is not working if the view has no bounds
       self.backgroundView = [[UIView alloc] init];
       self.backgroundColor = [UIColor clearColor];
       self.selectedBackgroundView = [[UIView alloc] init];
       self.selectedBackgroundView.backgroundColor = [UIColor clearColor];
       self.clipsToBounds = YES;
       self.contentView.clipsToBounds = YES;

       // Add empty view
       //emptyView = [[View1 alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) andRadius:CELL_DOT_RADIUS];//dont set the frame heare do it layoutsubviews

       emptyView = [[View1 alloc] init];
       emptyView.userInteractionEnabled = NO;
       emptyView.tag = 100;
      [self.backgroundView addSubview:emptyView];


  - (void)layoutSubviews
    {
         [super layoutSubviews];

         UIView *emptyView = [self viewWithTag:100];//your background view heare u can get the frame values for ur cell
         emptyView.frame = self.bounds;//better do it for other views in cell also 
          //test it by setting some background color 

 }


Hope this helps u ...:)

Shankar BS
  • 8,394
  • 6
  • 41
  • 53
  • Don't think it matters so much but you're right, i should add this because initially the cell has 44px height, the default cell height. The content of a view should be visible even if has 1px. I added the sources if you want to play. Thanks. – Cristi Băluță Jan 10 '14 at 10:10
0

Ok, so like i've said in my last comment the backgroundColor of any subview is the problem, in the selected state is cleared. For cells created in interface builder the problem does not occurs, my cells are created programatically. I fixed this by drawing with CoreGraphics in drawRect method.

Cristi Băluță
  • 1,295
  • 15
  • 27
0

As with a UICollectionView a UITableView has a background view that is build before AutoLayout. If you use autoLayout you should make sure the backgroundView has a frame. Otherwise is will disappear. In your cell subclass you can do:

    -(void)setBackgroundView:(UIView *)backgroundView
{
    super.backgroundView = backgroundView;
    self.backgroundView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.backgroundView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
}
doozMen
  • 690
  • 1
  • 7
  • 14
0

In swift you need to declare viewcontroller object globally that would result in Strong, in case if you declare locally it results in keep disappearing the cells.

var refineViewController : RefineViewController?

then you can access that controller using below code that would not result in disappearing cells.

func showRefineView(isFindHomeTab isFindHomeTab : Bool){


    refineViewController =   RefineViewController(nibName: String(BaseGroupedTableVC),bundle : nil)

    refineViewController!.refineSearchDelegate = self

    refineViewController!.view.frame = CGRectMake(0, -490, self.view.frame.size.width, self.view.frame.size.height)

    UIView.animateWithDuration(0.3, delay: 0.0, options: .CurveEaseOut, animations:
        {

            self.refineViewController!.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
            self.refineViewController!.isFindHomeTab = isFindHomeTab

        }, completion: nil)

        self.view.addSubview(refineViewController!.view)

}
aqsa arshad
  • 801
  • 8
  • 27