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

2

最好将这个很棒的库用于 gridview 如何在项目中实现和集成所有内容都记录在此链接上。看看这个 AQGridview

于 2012-12-03T06:32:43.693 回答
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

你可以试试 UIcollectionView 一个新的 ios 控件,它类似于 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 回答