表视图不可见

Posted

技术标签:

【中文标题】表视图不可见【英文标题】:Table View not visible 【发布时间】:2013-11-25 05:41:53 【问题描述】:

我已经在 xcode 4 中完成了我的应用程序编码,但我正在 xcode 5 上尝试此代码,UITableview 中出现了许多问题。表格行和表格内容不可见。通常表格显示正确,但我在表格视图中为子视图设置了水平线和分隔线,该时间框架仅设置线和内容不可见。

在图片中,我将 tableview 的背景颜色设置为蓝色,因此这里的内容区域仅以白色显示


        UIView *subView=[[UIView alloc]initWithFrame:CGRectMake(5, 0, 510, ([subViewsDataArr count] * 50) + 10)];
        subView.backgroundColor=[UIColor clearColor];
        subView.layer.borderWidth = 1;
        subView.layer.borderColor = [[UIColor blackColor] CGColor];
        subView.layer.cornerRadius=5;
        [myCell addSubview:subView];

        int y = 5;
        for (int j=0; j<[subViewsDataArr count]; j++, y = y+50) 
            UIView *cellView =[[UIView alloc] init];
            cellView.frame =CGRectMake(5, y, 500, 50);
            cellView.backgroundColor =[UIColor clearColor];
            [subView addSubview:cellView];

            if (j == 0)
            
                int x =-1;

                UIImageView *horizontalLineImgStart =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 500, 1)];
                horizontalLineImgStart.image =[UIImage imageNamed:@"horizontalLine.png"];
                [cellView addSubview:horizontalLineImgStart];

                UIImageView *seperatorLineImgStart =[[UIImageView alloc] initWithFrame:CGRectMake(x, 0, 1, 50)];
                seperatorLineImgStart.image =[UIImage imageNamed:@"seperatorLine.png"];
                [cellView addSubview:seperatorLineImgStart];

                for (int i=0; i < 8; i++) 

                    UILabel *cellLbl = [[UILabel alloc] init];
                    [cellLbl setText:[NSString stringWithFormat:@"%@",[[subViewsDataArr objectAtIndex:j] objectAtIndex:i]]];
                    cellLbl.textAlignment =UITextAlignmentCenter;
                    if (i == 0 || i == 1 || i == 4 || i == 5 || i == 6 || i == 7) 
                        cellLbl.font =[UIFont fontWithName:@"Helvetica Bold" size:12];
                        cellLbl.numberOfLines =2;
                    
                    else
                    
                        cellLbl.adjustsFontSizeToFitWidth =YES;
                        cellLbl.font =[UIFont fontWithName:@"Helvetica Bold" size:10];
                    
                    [cellLbl setBackgroundColor:[UIColor clearColor]];
                    cellLbl.textColor =[UIColor grayColor];
                    [cellView addSubview:cellLbl];


                    UIImageView *seperatorLineImg =[[UIImageView alloc] init];
                    if (i == 0 || i == 1 || i == 2 || i == 5)
                    
                        cellLbl.frame = CGRectMake(x + 1, 5, 55, 40);
                        seperatorLineImg.frame =CGRectMake(x + 55, 0, 1, 50);
                        x = x + 55;
                    
                    else if (i == 6 || i == 7)
                    
                        cellLbl.frame = CGRectMake(x + 1, 5, 80, 40);
                        seperatorLineImg.frame =CGRectMake(x + 80, 0, 1, 50);
                        x = x + 80;
                    
                    else
                    
                        cellLbl.frame = CGRectMake(x + 1, 5, 60, 40);
                        seperatorLineImg.frame =CGRectMake(x + 60, 0, 1, 50);
                        x = x + 60;
                    
                    seperatorLineImg.image =[UIImage imageNamed:@"seperatorLine.png"];
                    [cellView addSubview:seperatorLineImg];

                

                UIImageView *horizontalLineImgEnd =[[UIImageView alloc] initWithFrame:CGRectMake(0, 50, 500, 1)];
                horizontalLineImgEnd.image =[UIImage imageNamed:@"horizontalLine.png"];
                [cellView addSubview:horizontalLineImgEnd];
            
            else
            
                int x =-1;

                UIImageView *horizontalLineImgStart =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 500, 1)];
                horizontalLineImgStart.image =[UIImage imageNamed:@"horizontalLine.png"];
                [cellView addSubview:horizontalLineImgStart];

                UIImageView *seperatorLineImgStart =[[UIImageView alloc] initWithFrame:CGRectMake(x, 0, 1, 50)];
                seperatorLineImgStart.image =[UIImage imageNamed:@"seperatorLine.png"];
                [cellView addSubview:seperatorLineImgStart];

            for (int i=0; i < 8; i++ , txtFldTagVal= txtFldTagVal + 1) 

                    UITextField *cellTxtFld = [[UITextField alloc] init];
                    cellTxtFld.text =[NSString stringWithFormat:@"%@",[[subViewsDataArr objectAtIndex:j] objectAtIndex:i]];
                    cellTxtFld.textAlignment =UITextAlignmentCenter;
                    cellTxtFld.font =[UIFont fontWithName:@"Helvetica Bold" size:12];
                    [cellTxtFld setBackgroundColor:[UIColor clearColor]];
                    cellTxtFld.textColor =[UIColor brownColor];
                    cellTxtFld.tag =5000 + txtFldTagVal;
                    cellTxtFld.delegate =self;
                    cellTxtFld.adjustsFontSizeToFitWidth = YES;
                    [cellView addSubview:cellTxtFld];

                    UIImageView *seperatorLineImg =[[UIImageView alloc] init];
                    if (i == 0 || i == 1 || i == 2 || i == 5)
                    
                        cellTxtFld.frame = CGRectMake(x + 1, 5, 55, 40);
                        seperatorLineImg.frame =CGRectMake(x + 55, 0, 1, 50);
                        x = x + 55;
                    
                    else if (i == 6 || i == 7)
                    
                        cellTxtFld.frame = CGRectMake(x + 1, 5, 80, 40);
                        seperatorLineImg.frame =CGRectMake(x + 80, 0, 1, 50);
                        x = x + 80;
                    
                    else
                    
                        cellTxtFld.frame = CGRectMake(x + 1, 5, 60, 40);
                        seperatorLineImg.frame =CGRectMake(x + 60, 0, 1, 50);
                        x = x + 60;
                    
                    seperatorLineImg.image =[UIImage imageNamed:@"seperatorLine.png"];
                    [cellView addSubview:seperatorLineImg];
                

                UIImageView *horizontalLineImgEnd =[[UIImageView alloc] initWithFrame:CGRectMake(0, 50, 500, 1)];
                horizontalLineImgEnd.image =[UIImage imageNamed:@"horizontalLine.png"];
                [cellView addSubview:horizontalLineImgEnd];
            
        
    

【问题讨论】:

用一些参考清楚地解释您的查询,然后只有您应该在这里获得任何帮助。 我正在创建带有水平和分隔线图像的表格视图,该时间表框架没有添加任何可见内容。 如果我在表格视图中设置了子视图,那么只有表格视图不可见。 @user3030120 : 你能写清楚的查询吗,我无法得到它。 【参考方案1】:

我认为您在表格单元格中添加了一些内容,例如 removeFromSuperView 以便只有该视图不可见,如果您的代码中有以下代码,请删除以下代码

[myCell.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; [myCell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

【讨论】:

是的,现在问题已经解决了。但是一旦我删除子视图,它就会合并另一个子视图

以上是关于表视图不可见的主要内容,如果未能解决你的问题,请参考以下文章

重新加载表视图数据,现有数据可见

表格视图单元格在设备上不可见

UIView 子类不可见

我的tableview单元格上的组件不可见

面试阙表视图重用标识符内部工作

如何在iOS中的表视图底部创建固定单元格?