我在列表视图中从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;
}
答案 0 :(得分:2)
最好将这个非常棒的库用于gridview 如何在项目中实施和集成所有内容都记录在此链接上。check this out AQGridview
答案 1 :(得分: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是你的scrollview将它添加到IB或以编程方式添加,因为在我的情况下我已经以编程方式添加它。根据您的要求设置内容大小和框架
答案 2 :(得分: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;
}
答案 3 :(得分:0)
你可以尝试UIcollectionView一个新的ios控件,它类似于UItabel视图,易于集成。 请查看这些链接
http://ashfurrow.com/blog/uicollectionview-example
http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12