选中时,视图从UITableViewCell消失

我有一个UITableView自定义UITableViewCells。 单元格的内容保存在一个单独的类中,扩展了UIView(我有3种内容,通过隐藏和显示视图来切换,因为我希望在单元格之间进行切换,所以我采用了这种方法)。 所以让我们假设我有一个可见的视图,并添加到单元格的contentView。 这个视图包含一些文字和由UIViews构成的一些矩形。 一切都很好直到现在,但奇怪的是当我触摸细胞,矩形简单地消失,文本保持不变。 你知道可能是什么问题吗? 我试图将视图添加到backgroundView,它也是这样做的。

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

风景:

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

这是源,所以你可以尝试自己,如果你想。 正如你所看到的橙色的矩形消失,但没有文字。 因为我需要做什么都不应该消失,在最好的情况下,我想改变他们的颜色。 http://ge.tt/6wRBObD1/v/0?c

在swift中,你需要全局声明viewcontroller对象,这会导致Strong,如果你在本地声明它会导致消失的单元格。

 var refineViewController : RefineViewController? 

那么你可以使用下面的代码访问该控制器,这不会导致消失的单元格。

 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) } 

您将其添加到背景视图。 选定后,选定的背景视图将显示不是背景视图。 在你的自定义单元格尝试

 - (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 } 

或者添加空视图到contentview

一个框架设置你需要重写“layoutsubviews”方法…在自定义单元格

  • 在初始化方法i中,初始化所有的视图,你的- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier方法,并将其添加到单元格

  • - (void)layoutSubviews只是为单元格中的ur视图设置框架,

使用标签属性来访问layoutsubviews中的视图,如果它不是属性的话

  - (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 } 

希望这可以帮助你… 🙂

好吧,就像我在上次评论中所说的那样,任何子视图的backgroundColor都是问题,在选中的状态下被清除。 对于在界面生成器中创build的单元格,问题不会发生,我的单元格将以编程方式创build。 我用drawRect方法用CoreGraphics绘图来解决这个问题。

与UICollectionView一样,UITableView有一个在AutoLayout之前构build的背景视图。 如果你使用autoLayout,你应该确保backgroundView有一个框架。 否则就会消失。 在你的单元格子类中,你可以这样做:

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