在TableView中显示图像

Rox*_*Rox 2 iphone objective-c uitableview ios5

我想在一个显示图像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;
}
Run Code Online (Sandbox Code Playgroud)

The*_*ger 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)];
}
Run Code Online (Sandbox Code Playgroud)

你可以像这样处理每个图像

-(void)buttonImagePressed:(id)sender
{
    NSLog(@"you have pressed : %d button",[sender tag]);
}
Run Code Online (Sandbox Code Playgroud)

(注意:首先,这取决于你和你的逻辑,所以请你的逻辑很强.没有人可以在这里写一切.如果你是一个初学者,那么请学习一段时间"所有最好的")