我已经开始使用IOS开发,并且是我的第一个客户端项目。
我被困在以下地方:
所以如果这个月有10个人的生日,我需要在一行中显示10个图像视图(3张图片,3张图片和1张图片)。
有人可以帮我解决上述问题。非常感谢。
答案 0 :(得分:3)
如果你想在tableview中添加图像/标签/按钮,那么最简单的方法是子类UITableViewCell。例如,如果你有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的子类化。现在如何在视图控制器中使用它们?
您必须在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;
}
这是解决问题背景的原型解决方案 希望它会对你有所帮助:)。
答案 1 :(得分:0)
- (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张图片),但你应该能够从那里弄明白。