次の機能が必要なアプリを開発しています。
- サーバーから複数の画像ファイルをダウンロードします。
- グリッド形式で表示します。
上記の要件を満たすために、
.xib ファイルでカスタム ビューを作成しました。それをスクロールビューに追加しました。
しかし、画像を非同期にダウンロードして適切な場所に表示する方法についてはわかりません。
グリッド スタイルでカスタム ビューを追加するための私のコードはこちらです。
-(void)LazyLoading_display_Objects_in_grid_style
{
for(int i=0; i<[imageListingArray count] ; i++)
{
NSArray *arr = [[NSBundle mainBundle] loadNibNamed:@"MyView" owner:nil options:nil];
MyView *myView = (MyView *)[arr objectAtIndex:0];
myView.frame = CGRectMake(x,y,width,height);
myView.imgPhoto setContentMode:UIViewContentModeScaleAspectFit];
myView.imageURL = [NSString stringWithFormat:@"%@",[[imageListingArray objectAtIndex:i] valueForKey:@"name"]];
[myView setBackgroundColor:[UIColor clearColor]];
if(myView.imgPhoto.image == nil)
[myView.actLoading startAnimating];
[myView.imgPhoto setTag:1000+i];
[myView setUserInteractionEnabled:TRUE];
myView.tag = 100+i;
[self.imageListingScrollView addSubview:myView];
if(current_no_of_object_in_row !=noOfObjectInColumn)
{
x =x + width + 8 ;
current_no_of_object_in_row ++;
}
else
{
current_no_of_object_in_row =1;
x = 5;
y = y+ height + 8 ;
}
scrollView_Height = y;
}
int temp = [imageListingArray count]%noOfObjectInColumn;
if(temp>0)
{
NSLog(@"No modulus");
y = y + height+ 10;
}
scrollView_Height = y;
NSLog(@"Scroll Height : %d",scrollView_Height);
[self.imageListingScrollView setContentSize:CGSizeMake(308, scrollView_Height+ 5)];
}