在滚动视图中隐藏文本视图

Posted

技术标签:

【中文标题】在滚动视图中隐藏文本视图【英文标题】:Hidding text view in scroll view 【发布时间】:2011-03-23 11:49:46 【问题描述】:

我正在开发一个应用程序,我在其中添加了文本视图和滚动视图中的按钮。 滚动视图是根据数据库的数据使用数组动态生成的。 现在我想做的是文本视图应该隐藏在我在滚动视图中添加的按钮单击上。

代码.........

-加载滚动视图.....

-(void) loadScrollView

cxs=0;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"dreambuilder.sqlite"];
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) 

    const char *sql;

    sql = "select  dname, dream from dream order by dorder";
    //btnNextDream.enabled = FALSE;
    sqlite3_stmt *statement;
    statement = nil;
    if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) 
    

        sqlite3_bind_int(statement, 1, objAppdelDreamDetail.intEditDream );
        while (sqlite3_step(statement) == SQLITE_ROW) 
        

            btnItem = [[UIButton alloc]init];
            btnItem.backgroundColor=[UIColor clearColor];
            txtViewDescription = [[UITextView alloc]init];
            CGRect rect;  
            rect.origin.x = ((myScroll.frame.size.width - 320) /2) + cxs ;
            rect.origin.y = 0;
            rect.size.height = 460;
            rect.size.width = 320;
            [btnItem addTarget:self action:@selector(actionShowDreamImage:) forControlEvents:UIControlEventTouchUpInside];
            btnItem.frame = rect;
            [btnItem setImage:[UIImage imageWithContentsOfFile:[documentsDirectory stringByAppendingPathComponent:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]]] forState:UIControlStateNormal]; 
            NSString *s=[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)] ;
            for(int i=0;i<[arrDream count];i++)
            
                txtViewDescription.tag=i;

            if ([s isEqual:@""]) 
                    txtViewDescription.frame=CGRectMake(rect.origin.x+15, 60, 300, 40);
                else 
                    CGSize textViewSize = [s sizeWithFont:[UIFont systemFontOfSize:18] constrainedToSize:CGSizeMake(300, 460) lineBreakMode:UILineBreakModeWordWrap];
                    txtViewDescription.frame=CGRectMake(rect.origin.x+15, 60, 300, textViewSize.height+10);
                    txtViewDescription.text=s;
                
                txtViewDescription.alpha = 0.5; 
                txtViewDescription.layer.cornerRadius=10.0;
                txtViewDescription.font=[UIFont fontWithName:@"Helvetica" size:18];
                txtViewDescription.backgroundColor=[UIColor blackColor];
                txtViewDescription.textColor=[UIColor whiteColor];
                txtViewDescription.editable = NO;
                txtViewDescription.scrollEnabled=FALSE;
            
            [myScroll addSubview:btnItem];
            [myScroll addSubview:txtViewDescription];
            [btnItem release];
            myScroll.scrollsToTop = NO;
            [txtViewDescription release];
            cxs += myScroll.frame.size.width;

        
    
    sqlite3_finalize(statement);

 
else 

    sqlite3_close(database);
    NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));


[myScroll setContentSize:CGSizeMake(cxs, [myScroll bounds].size.height)];
if (intOID=1) 
    btnPrevDream.enabled = FALSE;


int page;
if(objAppdelDreamDetail.intPageCount>0)

    page=objAppdelDreamDetail.intPageCount;
    //NSLog(@"%d",page);


else 

    page=objAppdelDreamDetail.intPageCount;
    //NSLog(@"%d",page);

[myScroll setContentOffset:CGPointMake(320 * page, 0) animated:YES];

按钮事件............

-(IBAction)actionShowDreamImage:(id) 发送者 如果(!flgFaded) tbarFooter.hidden = TRUE; tbarHeader.hidden = TRUE; lblNavTitle.hidden = TRUE; lblNavEdit.hidden = TRUE; lblNavPls.hidden = TRUE; //txtViewDescription.hidden = TRUE;

    for(int i=1;i<[arrDream count];i++)
    
        [[myScroll viewWithTag:1] setHidden:YES];
        txtViewDescription.tag=i;
        txtViewDescription.hidden=TRUE;

    


else

    tbarFooter.hidden = FALSE;
    tbarHeader.hidden = FALSE;
    lblNavTitle.hidden = FALSE;
    lblNavEdit.hidden = FALSE;
    lblNavPls.hidden = FALSE;
    txtViewDescription.hidden = FALSE;

flgFaded = !flgFaded;

【问题讨论】:

你能告诉我们什么不起作用吗? @ Erik B ....所有代码运行良好,我想隐藏所有文本 View is button click event. 【参考方案1】:

仅更改按钮单击事件方法

.......

-(IBAction)actionShowDreamImage:(id) sender 

if(!flgFaded)
    
        tbarFooter.hidden = TRUE;
        tbarHeader.hidden = TRUE;
        lblNavTitle.hidden = TRUE;
        lblNavEdit.hidden = TRUE;
        lblNavPls.hidden = TRUE;
        for(int i=1;i<[arrDream count];i++)



for(UITextView *txtview in myScroll.subviews)



if(txtview.tag==i)
                
                    txtview.hidden=YES;
                
            
        

    
    else
    
        tbarFooter.hidden = FALSE;
        tbarHeader.hidden = FALSE;
        lblNavTitle.hidden = FALSE;
        lblNavEdit.hidden = FALSE;
        lblNavPls.hidden = FALSE;
        for(int i=1;i<[arrDream count];i++)
        
            for(UITextView *txtview in myScroll.subviews)
            
                if(txtview.tag==i)
                
                    txtview.hidden=NO;
                
            

        
    
    flgFaded = !flgFaded;

【讨论】:

以上是关于在滚动视图中隐藏文本视图的主要内容,如果未能解决你的问题,请参考以下文章

滚动到滚动视图/滚动视图中的特定文本字段 当键盘在屏幕上时停止滚动

滚动表格视图时隐藏文本字段数据

如何在 UIView 中设置滚动视图以向上滑动被键盘隐藏的文本字段?

列表视图滚动时在屏幕底部隐藏线性布局

当新行添加到 UITextView 实例中时,如何向上滚动视图?

当用户滚动时如何显示或隐藏日期文本视图就像whatsapp聊天android Kotlin