首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将多个图像动态添加到TableView

将多个图像动态添加到TableView
EN

Stack Overflow用户
提问于 2012-05-01 21:38:25
回答 2查看 5K关注 0票数 0

我已经开始了IOS开发,并且正在做我的第一个客户项目。

我被困在下面的地方:

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

有没有人能帮我做上面的事。这将是非常感谢的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-01 22:54:53

如果你想在tableview中添加图片/标签/按钮,那么最简单的方法是将UITableViewCell.For子类化。例如,如果你有5张图片(不理解3张图片,3张图片和1张图片),并且你想在表行中显示它们,那么你可以这样做:

  1. 在项目中添加一个名为CustomCell的新文件,该文件应该是UITableviewCell的子类,如下所示:@interface CustomCell : UITableViewCell { UIImageView *imageView1;UIImageView *imageView2;UIImageView *imageView3;UIImageView *imageView4;UIImageView *imageView5;} @property (非原子,保留) UIImageView *imageView1;@property (非原子,保留) UIImageView *imageView2;@property (非原子,保留) UIImageView *imageView3;@property (非原子,保留) UIImageView *imageView4;@property (非原子,保留) UIImageView *imageView5;@end
  2. 在CustomCell.m中放入如下代码:@ imageView1,imageView2,imageView3,imageView4,imageView5;- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self =超级initWithStyle:样式重用标识符:重用标识符){ self.imageView1 = [UIImageView alloc :CGRectMake(10,8,50,45)];self.contentView addSubview:imageView1;self.imageView2 = [UIImageView alloc :CGRectMake(65,8,50,45)];self.contentView addSubview:imageView2;self.imageView3 = [UIImageView alloc :CGRectMake(120,8,50,45)];self.contentView addSubview:imageView3;self.imageView4 = [UIImageView alloc :CGRectMake(170,8,50,45)];self.contentView addSubview:imageView4;self.imageView5 = [UIImageView alloc :CGRectMake(225,8,50,45)];self.contentView addSubview:imageView5;} return self;}

那么你已经完成了UITableViewCell.Now的子类化,如何在你的视图控制器中使用它们?

您必须在cellForRowAtIndexPath委托方法中的视图控制器class.And中导入CustomCell.h,执行以下操作:

代码语言:javascript
复制
    - (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;  

你可以使用更好的是:

代码语言:javascript
复制
    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; 
}

这就是您的问题上下文的原型解决方案。

希望它能帮助您:)

票数 3
EN

Stack Overflow用户

发布于 2012-05-01 22:28:27

代码语言:javascript
复制
- (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张图片)是什么意思,但你应该能够从那里弄明白。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10398487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档