2017-06-21 5 views
0

Imageviewで複数の画像を表示するビューで作業しており、画像の数が動的です。 これらの画像をURLで取得していて、URLの文字列が配列に格納されています。サブビューがiosの画面境界から外れると、サブビューを下に移動する方法

すべての単一のイメージが表示されるために新しいイメージビューを作成するためのforループを取りました。画像ビューのフレームは、画像のサイズに基づいて設定されます。問題は、画像の数が5を超えると、6番目の画像が画面外に出るということです。しかし、配列に5つ以上のイメージがある場合、それを下に移動したい。ここでは、以下の

は、forループ

int x=0; 
for (int i=0;i<[items count]; i++) 
{ 
    NSLog(@"image name %@",[items objectAtIndex:i]); 
    NSLog(@"image url %@",[NSURL URLWithString:[NSString stringWithFormat:@"%@",[items objectAtIndex:i]]]); 

    NSString* urlTextEscaped = [[NSString stringWithFormat:@"%@",[items objectAtIndex:i]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
    NSData *imgData=[NSData dataWithContentsOfURL:[NSURL URLWithString:urlTextEscaped]];   

    UIImageView *imgV = [[UIImageView alloc]initWithImage:[UIImage imageWithData:imgData]]; 
    imgV.frame=CGRectMake(x, 0, 50, 50); 
    [cell.contentView addSubview:imgV]; 
    x=x+imgV.frame.size.width+10; 
} 
+0

なぜあなたは、テーブルを使用していないスクロールビューセクションの下バウンス垂直オプションビューまたはスクロールビュー?? –

答えて

1

この画像のようなシフト

int x=0; 
int y =0; 
for (int i=0;i<[items count]; i++) 
{ 
    NSLog(@"image name %@",[items objectAtIndex:i]); 
    NSLog(@"image url %@",[NSURL URLWithString:[NSString stringWithFormat:@"%@",[items objectAtIndex:i]]]); 

    NSString* urlTextEscaped = [[NSString stringWithFormat:@"%@",[items objectAtIndex:i]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
    NSData *imgData=[NSData dataWithContentsOfURL:[NSURL URLWithString:urlTextEscaped]];   

    UIImageView *imgV = [[UIImageView alloc]initWithImage:[UIImage imageWithData:imgData]]; 
    imgV.frame=CGRectMake(x, y, 50, 50); 
    [cell.contentView addSubview:imgV]; 
    if(x>cell.frame.size.width) 
    { 
     x =0; 
     y= y+imgV.frame.size.height+10; 
    } 
    else 
    { 
     x=x+imgV.frame.size.width+10; 

    } 
} 
0

あなたはscrollview内の画像やテーブルビューを表示することができます使用するImageViewの上の画像を表示するための私のコードであるので、そこに多くの画像があり、あなたが合うことができないとき画面内のすべての画像。それからそれはスクロール機能を持​​ちます。

0

あなたはストーリーボードを使用している場合は、あなたのViewControllerのビューにscrollviewを追加し、このサンプル・コードはあなたを助ける必要があり、その後使用しています。

#import "ViewController.h" 

@interface ViewController() 
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView; 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    [self setAutomaticallyAdjustsScrollViewInsets:YES]; 

    [self addView:15]; 
} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(void) addView:(NSInteger)numberOfViews 
{ 
    double x =10 , y=10; 

    for (NSInteger i=1; i <= numberOfViews; i++) { 
     UIView * view = [[UIView alloc] initWithFrame:CGRectMake(x, y, 50, 50)]; 
     y = y+70; 
     [view setBackgroundColor:[UIColor purpleColor]]; 
     [_scrollView addSubview:view]; 
    } 
} 
@end 

注:インターフェースビルダーでは、あなたのscrollviewをクリックし、属性の検査官と検査(ダニ)

関連する問題