2

リスト ビューで Web サービスから画像を取得していますが、それらをグリッド ビューで表示したいと考えています。リストビューで画像を表示しているので、グリッドビューでこれを行うにはどうすればよいですか? 助けてください.これは私のコードがどのように見えるかです:-

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *CELLIDENTIFIER=@"CELL";
    UITableViewCell *cell=nil;
    if (cell==nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELLIDENTIFIER] autorelease];
        CGRect imageFrame = CGRectMake(2, 8, 80, 60);
        NSURL *url = [NSURL URLWithString:[arrayListG objectAtIndex: [indexPath row]]];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *img = [[[UIImage alloc] initWithData:data] autorelease];
        UIImageView *customImage = [[[UIImageView alloc] initWithFrame:imageFrame] autorelease];

        customImage.image=img;
        [cell.contentView addSubview:customImage];
    }

    return cell;
}
4

4 に答える 4

1
Gridview = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, 320, 480)];//your frame        
Gridview.backgroundColor=[UIColor clearColor];
int row=0;
int column=0;
int rows=4;
int cols=5;
for (int i=0; i<rows*cols; i++)
{
    if((row%4==0)&&(row>0))
    {
        row=0;
        column++;
    } 
    else{
        row++; 
    }
CGRect imageFrame = CGRectMake(row*80+10, column*60+10, 80, 60);//your imageview frame
    NSURL *url = [NSURL URLWithString:[arrayListG objectAtIndex:i]];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *img = [[[UIImage alloc] initWithData:data] autorelease];
    UIImageView *customImage = [[[UIImageView alloc] initWithFrame:imageFrame] autorelease];

    customImage.image=img;
    [Gridview addSubView:customImage];

}
[Gridview setContentSize:CGSizeMake(rows*90, cols*70)];

Gridview はスクロールビューであり、IB に追加するか、プログラムで追加します。私の場合は、プログラムで追加しました。要件に応じてコンテンツのサイズとフレームを設定します

于 2012-12-03T06:57:20.333 に答える
0

以下のようにコードを変更してください。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *CELLIDENTIFIER=@"CELL";
    UITableViewCell *cell=nil;
    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELLIDENTIFIER];

        CGRect imageFrame = CGRectMake(2, 8, 80, 60);
        UIImageView *customImage = [[UIImageView alloc] initWithFrame:imageFrame];
        customImage.tag = 1;
        [cell.contentView addSubview:customImage];
    }
    UIImageView *customImage = (UIImageView *)[cell viewWithTag:1];

    NSURL *url = [NSURL URLWithString:[arrayListG objectAtIndex: [indexPath row]]];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *img = [[UIImage alloc] initWithData:data];
    customImage.image =img;

    return cell;
}
于 2012-12-03T06:16:21.607 に答える
0

iOS 用の新しいコントロールである UIcollectionView を試すことができます。これは UItabel ビューに似ており、統合が簡単です。これらのリンクをご覧ください

http://ashfurrow.com/blog/uicollectionview-example

http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12

于 2012-12-03T07:09:17.913 に答える