【问题标题】:Adding Multiple images dynamically to TableView将多个图像动态添加到 TableView
【发布时间】:2012-05-01 13:38:25
【问题描述】:

我已经开始着手进行 IOS 开发,并且正在进行我的第一个客户项目。

我被困在以下地方:

所以如果这个月有 10 个人的生日,我需要在一行中显示 10 个图像视图(3 张图片、3 张图片和 1 张图片)。

有人可以帮我解决上述问题。将不胜感激。

【问题讨论】:

  • 只需将鼠标悬停在向下投票箭头上即可查看拒绝投票的原因,然后再次阅读您的问题。这里的人会在你的路上帮助或指导你,而不是为你做这项工作。
  • @Vishal Aggarwal :- 详细描述你的要求。你想在表格视图单元格中显示图像吗?或在视图中?如果你想在一个表格视图单元格中显示 10 个图像,那么你必须实现两种不同的方法。 1- 自定义您的表格单元格或 2.- 创建“脉冲”样式滚动 - 水平滚动 UITableView 作为 UITableViewCell 的子视图。首先详细告诉我您的要求,然后我会尽力帮助您。我希望我们都能取得成功:-)
  • @psycho - 如果信息的缺乏冒犯了您,我很抱歉。我是刚刚完成训练的完整初学者。随着我的学习,我一定会变得更好。 :)
  • @M.A.Khan - 我想我需要 initWithStyle 方法。我没有意识到这一点。如果一切顺利,我将使用它并在此处发布我的代码。谢谢你回答我的问题
  • @Vishal Aggarwal :- 详细描述你的要求。你想在表格视图单元格中显示图像吗?或在视图中?如果你想在一个表格视图单元格中显示 10 个图像,那么你必须实现两种不同的方法。 1- 自定义您的表格单元格或 2.- 创建“脉冲”样式滚动 - 水平滚动 UITableView 作为 UITableViewCell 的子视图。首先详细告诉我您的要求,然后我会尽力帮助您。我希望我们都能取得成功:-)

标签: ios uitableview uiimageview


【解决方案1】:

如果你想在tableview中添加图片/标签/按钮,那么最简单的方法是继承UITableViewCell。例如,如果你有5张图片(不明白3张图片,3张图片和1张图片)并且你想将它们显示在表格行中,然后您可以执行以下操作:

  1. 在您的项目中添加一个名为 CustomCell 的新文件,它应该是 UITableviewCell 的子类,如下所示:

    @interface CustomCell : UITableViewCell {  
    
        UIImageView *imageView1;  
        UIImageView *imageView2;  
        UIImageView *imageView3;  
        UIImageView *imageView4;  
        UIImageView *imageView5;  
    }  
    @property (nonatomic, retain) UIImageView *imageView1;  
    @property (nonatomic, retain) UIImageView *imageView2;  
    @property (nonatomic, retain) UIImageView *imageView3;  
    @property (nonatomic, retain) UIImageView *imageView4;  
    @property (nonatomic, retain) UIImageView *imageView5;  
    @end  
    
  2. 在 CustomCell.m 中这样写代码:

    @synthesize imageView1, imageView2, imageView3, imageView4, imageView5;
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
    {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) 
        {           
            self.imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 8, 50, 45)];
            [self.contentView addSubview:imageView1];
    
            self.imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(65, 8, 50, 45)];
            [self.contentView addSubview:imageView2];
    
            self.imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(120, 8, 50, 45)];
            [self.contentView addSubview:imageView3];
    
            self.imageView4 = [[UIImageView alloc] initWithFrame:CGRectMake(170, 8, 50, 45)];
            [self.contentView addSubview:imageView4];
    
            self.imageView5 = [[UIImageView alloc] initWithFrame:CGRectMake(225, 8, 50, 45)];
            [self.contentView addSubview:imageView5];
        }
    
    return self;
    }  
    

所以你已经完成了 UITableViewCell 的子类化。现在如何在你的视图控制器中使用它们?
您必须在 View Controller 类中导入 CustomCell.h。在 cellForRowAtIndexPath 委托方法中执行以下操作:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
    {  
        static NSString *CellIdentifier = @"Cell";  

        //our custom cell  
        CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
        if (cell == nil) {  
            cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];  
        }  

        // Configure the cell.  

        //cell.imageView1.image = [UIImage imageNamed:@"testImg1"];  
        //cell.imageView2.image = [UIImage imageNamed:@"testImg2"];  
        //cell.imageView3.image = [UIImage imageNamed:@"testImg3"];  
        //cell.imageView4.image = [UIImage imageNamed:@"testImg4"];  
        //cell.imageView5.image = [UIImage imageNamed:@"testImg5"];  
        //return cell;  

你可以使用更好的是:

    NSMutableArray *imgArray = [[NSMutableArray alloc] initWithObjects:cell.imageView1, cell.imageView2,cell.imageView3, cell.imageView4, cell.imageView5, nil];

    for (int imageCounter = 0; imageCounter<5; imageCounter++) {
        [[imgArray objectAtIndex:imageCounter] setImage:[UIImage imageNamed:[NSString stringWithFormat:@"testImg%d", imageCounter]]];
            }  
return cell; 
}

这是针对您的问题上下文的原型解决方案。
希望对你有帮助:)

【讨论】:

  • 感谢您的帮助。我会尝试一下,如果它对我有用,我会更新。
【解决方案2】:
- (UITableViewCell *)tableView:(UITableView *)tTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        for (int t =0; t<cell.subviews.count; t=t) {
            if (![[[cell.subviews objectAtIndex:t].class]isEqualToString:@"UIImageView"]) {
                t++;
            }
            else {
                [((UIView*)[[cell.subviews objectAtIndex:t])removeFromSuperview];
            }
        }
    }
    for (int t = 0; t<10; t++) {
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(cell.frame.size.width/10*t, 0, cell.frame.size.width/10, cell.frame.size.height)];
        [imageView setImage:[UIImage imageNamed:<#(NSString *)#>]];
        [cell addSubview:imageView];
    }
    return cell;
}

我不知道你的意思(3张图片,3张图片和1张图片),但你应该能够从那里弄清楚。

【讨论】:

  • 看起来你建议每次表格视图要求一行......每次......即使是刚刚滚动关闭然后重新打开的行,你建议他分配了 10 个 UIImage 视图并将它们作为子视图堆积起来。这对含糊的问题不是过于严厉的惩罚吗?
  • 您还想删除旧的或重新使用它们。另一种方法是创建一个 UITableViewCell 的子类,其中包含您可以设置的 10 个图像视图,但是您最多需要在每个单元格中放置 10 张图片。
  • 如果您想要未定义数量的图片,请尝试我给您的内容并对其进行修改以重复使用对象而不是垃圾并重新制作它们。如果不使用stackoverflow.com/a/10399483/1354623
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 2011-07-24
  • 1970-01-01
  • 2011-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多