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

发布于 2024-11-14 17:54:11 字数 175 浏览 1 评论 0原文

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

谢谢, 克里斯蒂

I am creating an app in which i want to display a table view and each cell of table view will be displaying 5 different images.Let me say in detail.I have 10 images and i want to show them in a table view .say 4 images in first row of table view then next 4 in next row ,then the remaining two in next row.Being a newbie i am unable to get a startup.Please help.Any help will be appreciated.

Thanks,
Christy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔嚣张 2024-11-21 17:54:11

我认为对于启动你可以在这里

如何以编程方式显示 UITableView?

好吧,如果您知道如何加载 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用户习惯通过查看表格右侧的滚动条来查看表格是否还有数据。

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

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

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]
}

I think for startup you can go over here

How to display the UITableView programmatically?

Ok if you know how to load a uitableview than I think you can get started with complex part.

You need to add this line of code in your cellForRow method

You need to set the frame size and image name according to your requirement

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

Edit:

@Christy you can have a button that can make a table view scroll. But iphone users are used to see the scroll bar on the right of the table to see if the table has any more data or not.

But if you want to have a Button that scrolls the table than here is the code

In your cellForRow method of tableView in this block else if(indexPath.row == 2){} add a UIButton with a down arrow like this

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];

and in the selector method

- (void)doSomething
{
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES]
}
就此别过 2024-11-21 17:54:11
    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];

}

    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];

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文