我想在TableView
中显示图片。我可以在一个单元格中显示一个图像,但是我希望在每一行中显示四个图像而不是一个图像,如照片库,当我选择任何图像时,它将像照片库中那样以幻灯片形式打开。 / p>
我的图片存储在文档目录中。我怎么能这样做?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tablView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//Adjust your imageview frame according to you
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0, 0.0, 470.0, 80.0)];
[imageView setImage:[arrayOfImages objectAtIndex:indexPath.row]];
[cell.contentView addSubview:imageView];
return cell;
}
答案 0 :(得分:5)
现在我只是解释如何显示像iPhone库这样的图像。使用 UIScrollView 代替 UITableView 。我认为这会更好!
获取数组中的所有图像后,请尝试以下代码:
-(void)loadImagesOnScrollView
{
myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 416.0)];
myScrollView.delegate = self;
myScrollView.contentSize = CGSizeMake(320.0, 416.0);
myScrollView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:myScrollView];
float horizontal = 8.0;
float vertical = 8.0;
for(int i=0; i<[imageArray count]; i++)
{
if((i%4) == 0 && i!=0)
{
horizontal = 8.0;
vertical = vertical + 70.0 + 8.0;
}
buttonImage = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonImage setFrame:CGRectMake(horizontal, vertical, 70.0, 70.0)];
[buttonImage setTag:i];
[buttonImage setImage:[imageArray objectAtIndex:i] forState:UIControlStateNormal];
[buttonImage addTarget:self action:@selector(buttonImagePressed:) forControlEvents:UIControlEventTouchUpInside];
[myScrollView addSubview:buttonImage];
horizontal = horizontal + 70.0 + 8.0;
}
[myScrollView setContentSize:CGSizeMake(320.0, vertical + 78.0)];
}
你可以像这样处理每个图像
-(void)buttonImagePressed:(id)sender
{
NSLog(@"you have pressed : %d button",[sender tag]);
}
(注意:首先,这取决于你和你的逻辑,所以请你的逻辑强大。没有人可以在这里写一切。如果你是初学者,那么请做一段时间的学习“All The Very Best “强>)
答案 1 :(得分:0)
您需要创建和布局新的UIView并将图像添加到其中。然后,您可以将该视图添加到单元格的contentView
,而不是当前正在添加的UIImageView
(仅包含一个图像)。
答案 2 :(得分:0)
实现每行包含四个图像的表格视图,根据行标记每个图像。为每个图像添加点击识别器,并根据图像标签执行您想要的代码。以下是代码,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellidentifier=@"cell";
UITableViewCell *cell=(UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellidentifier];
if(cell==nil)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellidentifier];
}
int x=0;
//configure the cell
for(int i=0;i<4;i++)
{
UIImage *image=[UIImage imageNamed:@"hh.jpg"];
UIImageView *im=[[UIImageView alloc]initWithImage:image];
im.frame=CGRectMake(0+x, 0, 60, 60);
x=x+61;
im.tag=(indexPath.row*10)+i;
im.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[im addGestureRecognizer:tapGesture];
[cell addSubview:im];
}
//cell.backgroundView=img;
[[cell textLabel] setBackgroundColor:[UIColor clearColor]];
[[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]];
return cell;
}
- (void) tapAction:(UIGestureRecognizer *)recognizer
{
NSLog(@"Clicked Button With Tag =%i",recognizer.view.tag);
}
答案 3 :(得分:0)
您必须先设置数据。也许一系列数组将是一个很好的方法。
-(void)prepareData
{
NSMutableArray *finalArray = [[NSMutableArray alloc]init];
int dataPerCell = 4;
int remainingData = [allImagesData count] % dataPerCell;
int pagesForData = [allImagesData count] / dataPerCell;
NSMutableArray *arrayToAdd = [[NSMutableArray alloc]init];
for (int j = 0; j<[allImagesData count]; j++)
{
[arrayToAdd addObject:[allImagesData objectAtIndex:j]];
//Check if dividing the array is needed
if ([allImagesData count] > dataPerCell)
{
if ([arrayToAdd count] == dataPerCell)
{
NSMutableArray *finalArray = [[NSMutableArray alloc]initWithArray:arrayToAdd];
[arrayToAdd removeAllObjects];
[finalArray addObject:finalArray];
}
else if([arrayToAdd count] == remainingData && [finalArray count] == pagesForData)
{
NSMutableArray *finalArray = [[NSMutableArray alloc]initWithArray:arrayToAdd];
[arrayToAdd removeAllObjects];
[finalArray addObject:finalArray];
}
}
else if([allImagesData count] <= dataPerCell)
{
NSMutableArray *finalArray = [[NSMutableArray alloc]initWithArray:arrayToAdd];
if ([finalArray count] == [allImagesData count])
{
[finalArray addObject:finalArray];
}
}
}
}
上面的代码将为您提供另一个数组的数组(finalArray),每个数组都有四个数据。
您可以创建UITableViewCell
的类子类,并向其添加属性,例如四个UIImageViews
,并将它们放置到位置。我假设在你的情况下,将它们水平放置。
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [finalArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
cell = [[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
//This will throw an exception if [allImagesData count] < 4 OR your [allImagesData count] % 4 != 0
[cell.imageVw1 setImage:[UIImage imageNamed:[[finalArray objectAtIndex:indexPath.row ] objectAtIndex:0]]];
[cell.imageVw2 setImage:[UIImage imageNamed:[[finalArray objectAtIndex:indexPath.row ] objectAtIndex:1]]];
[cell.imageVw3 setImage:[UIImage imageNamed:[[finalArray objectAtIndex:indexPath.row ] objectAtIndex:2]]];
[cell.imageVw4 setImage:[UIImage imageNamed:[[finalArray objectAtIndex:indexPath.row ] objectAtIndex:3]]];
return cell;
}
注意:代码未经过测试。
答案 4 :(得分:-1)
您需要创建自定义单元格子类 - 请参阅教程 Custom UITableViewCells with Interface Builder 。另外,我建议对Cocoa Touch框架进行进一步的研究和研究。祝你好运。