在表视图中将图像添加到单元格

时间:2011-06-08 12:21:48

标签: cocoa-touch xcode ipad uitableview uiimageview

我正在创建一个应用程序,其中我想显示一个表视图,表视图的每个单元格将显示5个不同的图像。让我详细说。我有10个图像,我想在表格视图中显示它们.say 4个图像在第一行的表视图中然后在下一行中接下来的4个,然后在下一行中剩下的两个。作为一个新手我无法获得启动。请帮助。任何帮助将不胜感激。

谢谢, 克里斯蒂

2 个答案:

答案 0 :(得分:1)

我认为对于初创公司你可以去这里

How to display the UITableView programmatically?

好的,如果您知道如何加载uitableview,我认为您可以开始使用复杂的部分。

您需要在cellForRow方法中添加这行代码

您需要根据您的要求设置帧大小和图像名称

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:cellIdentifier];
    }
    if(indexPath.row == 0)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image1.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image2.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image3.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image4.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];
    }
    else if(indexPath.row == 1)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image5.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image6.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image7.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image8.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];
    }
    else if(indexPath.row == 2)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image9.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image10.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];        
    }
    else
    {
        // Do something here
    }
    return cell;
}

编辑:

@Christy你可以有一个可以使表格视图滚动的按钮。但iphone用户习惯于查看表格右侧的滚动条,看看该表格是否还有其他数据。

但是如果你想要一个滚动表格的按钮,那么这就是代码

在此块else if(indexPath.row == 2){}中tableView的cellForRow方法中添加一个带有向下箭头的UIButton

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(220.0f, 5.0f, 150.0f, 38.0f)];
[button setImage:[UIImage imageNamed:@"Arrow"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(doSomething) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
[button release];

并在选择器方法

- (void)doSomething
{
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES]
}

答案 1 :(得分:1)

    IBOutlet UIImageView *image,*image12;

    In .m file
cellforrowAtIndexPath
{  
  UIImage *image1=[UIImage imageNamed:@"name.jpg"];
    image=[[UIImageview alloc]initWithImage:image1];
    image.frame=CGRectMake(0,0,50,50);

    [cell addSubview:image];

UIImage *image13=[UIImage imageNamed:@"name.jpg"];
    image12=[[UIImageview alloc]initWithImage:image13];
    image12.frame=CGRectMake(50,0,50,50);

    [cell addSubview:image12];

}