2

我想在TableView. 我可以在一个单元格中显示一张图像,但我想显示四张图像,而不是像照片库那样在每一行中显示一张图像,当我选择任何图像时,它将以幻灯片形式打开,就像照片库中发生的那样。

我的图像存储在文档目录中。我怎样才能做到这一点?

-(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;
}
4

5 回答 5

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”

于 2012-07-03T09:16:08.993 回答
0

您需要创建和布局一个新的 UIView 并将您的图像添加到其中。然后,您可以将该视图添加到单元格的contentView中,而不是UIImageView您当前正在添加的(仅包含一个图像)。

于 2012-07-03T08:56:28.883 回答
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);
}
于 2012-07-03T09:35:04.433 回答
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,并向其添加属性,例如 4 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;
}

注意:代码未经测试。

于 2012-07-03T09:36:33.090 回答
-1

您将需要创建一个自定义单元子类 - 请参阅教程Custom UITableViewCells with Interface Builder。另外,我建议对Cocoa Touch框架做一些进一步的研究和研究。祝你好运。

于 2012-07-03T08:55:06.417 回答