2011-10-30 2 views
2

私は2つのフィールド(画像のURLと説明)を含むXML文書を読み込むアプリケーションを持っています。ドキュメントに有効なイメージURLを持つフィールドがある場合は、ビューにイメージを表示する必要があります。そうでなければ、その説明を表示したいだけです。UIImageViewをビューに配置して項目を動的に追加する方法は?

私が午前問題は、次のとおりです。

  1. UIImageViewを表示する方法を動的に私はUIImageView

を追加して下向きになりましたので、UITextViewを移動する方法

  • 現在、私はちょうどビューを持っていますその上にUITextViewがあります。

    アイデア?

  • 答えて

    2

    1)-[UIView setHidden:] UIImageViewが既に存在する場合。そうでない場合は[[UIImageView alloc] initWithFrame:]を追加し、画像ビューをサブビューとしてビューに追加します。

    2)-[UIView setFrame:](一つの選択肢である)

    1

    以下は、あなたが何ができるかです。

    は、このようなUIImageViewとのviewDidLoadでUITextViewとビュー準備:

    -(void) viewDidLoad) 
    { 
        [super viewDidLoad]; 
        myImageView = [[UIImageView alloc] init]; 
        myImageView.frame = CGRectMake(x,y,0,0); //you can set it at the right position and even set either the width OR the height depending on where you want the textView in my example I'm gonna assume its beneath the imageView 
        //other imageView settings 
        [myView addSubView:myImageView]; 
    
        myTextView = [[myTextView alloc] init]; 
        myTextView.frame = CGRectMake(x,y,width,heigh+CGRectGetMaxY(myImageView.frame)); //here you prepare the textView for the imageView and the space it takes. But doesn't get hindered by it if it's not there. 
        //other textView settings. 
        [myView addSubView:myTextView]; 
    
        //You would want this loading function about here OR after this loading has been occured. 
        [self loadImageFromURL]; 
    } 
    
    -(void)loadImageFromURL 
    { 
        //get your image here.. or not 
    
        if(imageHasLoaded) //some condition that gets set when your image has successfully loaded. (This should be done in a delegate (didLoadImageFromURL) if you have such thing prepared. 
        { 
         //myImageView.image = theLoadedImage; 
         myImageView.frame = CGRectMake(x,y,theLoadedImageWidth,theLoadedImageHeight); 
         //after you set the frame of the imageView you need to refresh the view 
         [myView setNeedsDisplay]; 
        } 
    } 
    

    をそして、これはあなたが動的にいくつかのビューに画像やフレームとImageViewのを追加する方法です。