UIView或UIScrollView不会刷新以编程方式创建的控件上的显示

问题描述 投票:1回答:1

我正在以戏剧性方式创建一些控件,但是它们没有出现,它需要某种刷新方法。这是我的布局:“在此处输入图像描述”

这是聊天室,所有这些消息都是我在事件发生时动态创建的,我在启动时创建的内容确实会出现,但是即使代码正常,之后我创建的内容也无法显示,

这是我创建控件的方式(通过调用以下函数):

- (void) AddNewMessage: (NSString *)msg date1:(NSString* ) date2  {

    NSString *backimage =[NSString stringWithString:@""];
    NSString *who =[NSString stringWithString:@"1"];

    calculatedHeight=floor( msg.length / 40 )*20;
        if (calculatedHeight<30) calculatedHeight=30;
    if ([who isEqualToString:@"1"]) { 
        left=85;
        left_hour_button=25;
        backimage=@"green_bubble.png";
    }
    else {
        left=5;
        left_hour_button=240;
        backimage=@"grey_bubble.png";
    }

    UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(left, lastcalculatedHeight-10, 230, calculatedHeight+10)];   
    [button1 setBackgroundImage:[[UIImage imageNamed:backimage] stretchableImageWithLeftCapWidth:9 topCapHeight:13] forState:UIControlStateDisabled];
    [button1 setEnabled:FALSE];          
    [scrollView addSubview:button1];
    [button1 release];

    UIButton *buttonTime = [[UIButton alloc] initWithFrame:CGRectMake(left, lastcalculatedHeight+40, 50, 20)];  
    [buttonTime setBackgroundImage:[[UIImage imageNamed:@"hour_bubble.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:13] forState:UIControlStateDisabled];
    [buttonTime setFrame:CGRectMake(left_hour_button, lastcalculatedHeight+calculatedHeight-30, 55, 25)];
    [buttonTime setTitle:date2 forState:UIControlStateDisabled];                
    [buttonTime setTitleColor:[UIColor blackColor] forState:UIControlStateDisabled];
    buttonTime.titleLabel.font=[UIFont fontWithName:@"Helvetica" size:8.0]; 
    buttonTime.titleLabel.lineBreakMode= UILineBreakModeWordWrap;
    [buttonTime setEnabled:FALSE];
    [scrollView addSubview:buttonTime];
    [buttonTime release];


    UILabel *labelMessage = [[UILabel alloc] initWithFrame:CGRectMake(left+5, lastcalculatedHeight-5, 220, calculatedHeight)];           
    [labelMessage setText:msg];
    [labelMessage setFont:[UIFont fontWithName:@"Arial" size:12.0]];
    [labelMessage setLineBreakMode:UILineBreakModeWordWrap];
    [labelMessage setTextColor:[UIColor blackColor]];
    [labelMessage setAdjustsFontSizeToFitWidth:NO];
    [labelMessage setNumberOfLines:floor( msg.length / 40 )+2];
    [labelMessage setBackgroundColor:[UIColor clearColor]];
    [scrollView addSubview:labelMessage];
    [labelMessage release];

    lastcalculatedHeight+=calculatedHeight+20;

    totalHeight+=calculatedHeight;  

    scrollView.contentSize = CGSizeMake(240, lastcalculatedHeight);
}

如何“刷新”显示?

scrollView是Uiview内部的一个对象,该类从该对象派生

此外,每次添加新消息时,我都希望向下滚动到滚动视图的底部。

我见过this post,但是它不起作用,或者在模拟器上的行为很奇怪...尚未在设备上尝试过,因为我还没有iPhone。

它滚动太多或根本没有滚动,但是如果我转到另一个选项卡,然后再切换回去,滚动条将在底部滚动,甚至显示我以编程方式创建的控件...是不是很奇怪?

iphone uiscrollview refresh scroll
1个回答
0
投票

如果self是视图控制器,或者只是[self.view setNeedsDisplay],请尝试使用[scrollView setNeedsDisplay]。>

© www.soinside.com 2019 - 2024. All rights reserved.