【问题标题】:Adding images to a cell in table view将图像添加到表格视图中的单元格
【发布时间】:2011-09-10 20:08:54
【问题描述】:

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

谢谢, 克里斯蒂

【问题讨论】:

    标签: cocoa-touch xcode ipad uitableview uiimageview


    【解决方案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]
    }
    

    【讨论】:

    • @robin:我认为在两个链接中都没有关于添加图像的详细信息。
    • 那是因为@Christina 询问如何开始,我认为这些是比在单元格中添加图像更重要的事情
    • 将 5 张图片添加到我想要的单元格中,请告诉
    • 有什么办法可以像箭头一样添加滚动按钮来向用户显示他可以向下滚动。我想要第二行中的那个功能。
    • 说我的表格视图是空的,我在第一行添加了一个图像,然后我同时将图像添加到相应的行。我的表格视图只能显示两行,所以当图像输入到第三行然后我想在第三行旁边有一个滚动按钮,它向用户显示下面有要滚动的数据。请给出一个想法。
    【解决方案2】:
        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];
    

    }

    【讨论】:

    • 我想在一个单元格中添加 5 张图片。如何做到这一点
    • @christina: 像这样分配 5 张图片并将其添加到单元格中。
    • 在什么情况下必须写这个
    • 对不起,我在这方面是个新手
    • 我在做同样的事情但是说错误:-UIImage *image=[UIImage imageNamed:@"BEHIND.png"]; imageView=[[UIImageview alloc]initWithImage:image]; imageView.frame=CGRectMake(0,0,320,400); [单元格添加子视图:图像视图];单元格文本 = 单元格值;返回单元格;