Views disappear from UITableViewCell when selected

I have a UITableView with custom UITableViewCells. The contents of the cell are stored in a separate class that extends the UIView (I have 3 types of content that I switch, hiding and showing views, I used this approach because of the way I wanted to make the transition between cells). Therefore, let's say that I have one of the views visible and added to the contentView cell. This view contains text and some rectangles from UIViews. Everything is fine so far, but it’s strange when I touch the cell, the rectangles just disappear, the text remains the same. Do you know what could be the problem? I also tried adding a view to the backgroundView by 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]; 

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]; 

These are sources, so you can try it yourself if you want. As you can see, the orange rectangle disappears, but the text does not. For what I need to do, nothing should disappear, at best I want to change their colors. http://ge.tt/6wRBObD1/v/0?c

0
source share
5 answers

In swift, you need to declare the viewcontroller object globally, which will lead to strong, in case you declare locally, this will lead to the disappearance of cells.

 var refineViewController : RefineViewController? 

then you can access this controller using the code below that will not cause the cells to disappear.

 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) } 
+1
source

You add it to the background view. When selected, the selected background view will not be displayed in the background. 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 an empty view to the contentview

0
source

one for frame settings u need to override the layoutsubviews ... method in a custom cell

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

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

use a tag property to access views in layoutsubviews if this is not a 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 you ... :)

0
source

Ok, so, as I said in my last comment, the backgroundColor of any subview is a problem, it is cleared in the selected state. For cells created in the interface constructor, the problem does not arise, my cells are created programmatically. I fixed this by drawing with CoreGraphics in the drawRect method.

0
source

As with UICollectionView, UITableView has a background view that is created before AutoLayout. If you use autoLayout, you must make sure that backgroundView has a frame. Otherwise it will disappear. In a subclass of your cell, you can:

  -(void)setBackgroundView:(UIView *)backgroundView { super.backgroundView = backgroundView; self.backgroundView.translatesAutoresizingMaskIntoConstraints = NO; [self.backgroundView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero]; } 
0
source

Source: https://habr.com/ru/post/988712/


All Articles