我已经开始使用IOS开发,并且在我的第一个客户端项目上。动态添加多个图像到TableView
我坚持在下列地点:
所以如果有这个月是10人的生日,我需要显示在一个单行10个imageviews(3张照片,3张图片和1张图片)。
有人可以帮我解决上述问题。这将不胜感激。
我已经开始使用IOS开发,并且在我的第一个客户端项目上。动态添加多个图像到TableView
我坚持在下列地点:
所以如果有这个月是10人的生日,我需要显示在一个单行10个imageviews(3张照片,3张图片和1张图片)。
有人可以帮我解决上述问题。这将不胜感激。
如果你想在tableview中添加图像/标签/按钮,那么最简单的方法是子类UITableViewCell.For例如,如果你有5个图像(不明白3张图片,3张图片和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
而且在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.Now的子类做了如何在您的视图控制器使用它们?
你必须导入CustomCell.h在视图控制器class.And在的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;
}
这是针对问题背景的非常原型的解决方案。
希望它能帮助你:)
- (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视图并将它们作为子视图进行堆叠。这不是对这个模糊问题的过度苛刻的惩罚吗? – danh
你也想删除旧的或重新使用它们。 另一种方法是创建一个具有10个图像视图的UITableViewCell的子类,您可以设置,但是然后您需要最多可以放置在每个单元中的10张图片。 –
如果你想要一个未定义数量的图片,请尝试我给你的并修改它以重用对象而不是垃圾并重新制作它们。 如果不使用http://stackoverflow.com/a/10399483/1354623 –
只需将鼠标悬停在downvote箭头上,看看downvoting的原因,然后再次阅读你的问题。这里的人会帮助或指导你,而不是为你工作。 – psycho
@Vishal Aggarwal: - 详细描述你的要求..你想在表格视图单元格中显示图像吗?或在一个视图? 如果你想在一个表格视图单元中显示10个图像,那么你必须实现两种不同的方法。 1-自定义你的表格单元格或2.-创建'脉冲'风格滚动 - 水平滚动UITableView作为UITableViewCell的子视图。 首先告诉我你的要求详细后,我会尽力帮助你。我希望我们都能取得成功:-) – 2012-05-01 14:10:09
@psycho - 如果缺乏信息让你感到不快,我很抱歉。我完成了刚完成训练的初学者。就我所知,我一定会变得更好。 :) –