选中后,视图会从UITableViewCell中消失

时间:2014-01-10 08:31:00

标签: ios uiview uitableview

我有一个带有自定义UITableViewCells的UITableView。单元格的内容保存在一个单独的类中,扩展了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

5 个答案:

答案 0 :(得分:1)

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

}

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

框架设置的一件事你需要覆盖“layoutsubviews”方法...... 在自定义单元格中

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

  • - (void)layoutSubviews
  • 只需在单元格中设置您的视图框架,

使用tag属性访问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 

 }


希望这有助于你......:)

答案 3 :(得分:0)

好的,就像我在上次评论中所说的那样,任何子视图的backgroundColor都是问题,在选中状态被清除。对于在界面构建器中创建的单元格,不会发生问题,我的单元格是以编程方式创建的。 我通过在drawRect方法中使用CoreGraphics绘制来修复此问题。

答案 4 :(得分:0)

与UICollectionView一样,UITableView具有在AutoLayout之前构建的背景视图。如果使用autoLayout,则应确保backgroundView具有框架。否则就会消失。 在您的单元子类中,您可以执行以下操作:

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