【发布时间】:2011-12-28 16:35:04
【问题描述】:
我需要在表格视图中每行显示三个图像。我创建了一个自定义 tableviewcell,其中包含三个图像视图。其 cellForRowAtIndexPath: 方法中的 tableView 将图像的名称提供给单元格,并且单元格应在一行中并排加载它们。但是图像没有出现在 tableView 中。我不确定为什么它不起作用。请帮忙!
代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
customCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSString *imageForCell = [arrayImages objectAtIndex:(indexPath.row*3)];
cell.firstImageName = imageForCell;
imageForCell = [arrayImages objectAtIndex:(indexPath.row*3)+1];
cell.secondImageName = imageForCell;
cell.thirdImageName = [arrayImages objectAtIndex:(indexPath.row*3)+2];
return cell;
}
customCell.m:
#import "customCell.h"
@implementation customCell
@synthesize firstImageView, secondImageView,thirdImageView, firstImageName, secondImageName, thirdImageName;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
//Make the Rects for the images
CGRect rectForFirstImage = CGRectMake(10, 0, 90, 140);
CGRect rectForSecondImage = CGRectMake(110, 0, 90, 140);
CGRect rectForThirdImage = CGRectMake(210, 0, 90, 140);
//create the Image views with the Rects
firstImageView.frame = rectForFirstImage;
secondImageView.frame = rectForSecondImage;
thirdImageView.frame = rectForThirdImage;
//set the images
[firstImageView setImage:[UIImage imageNamed:firstImageName]];//tried setting the image this way also
// firstImageView.image = [UIImage imageNamed:firstImageName];
secondImageView.image = [UIImage imageNamed:secondImageName];
thirdImageView.image = [UIImage imageNamed:thirdImageName];
// set the content mode for the image views to fit in the images
firstImageView.contentMode = UIViewContentModeScaleAspectFit;
secondImageView.contentMode = UIViewContentModeScaleAspectFit;
thirdImageView.contentMode = UIViewContentModeScaleAspectFit;
}
return self;
}
-(void) layoutSubviews{
[super layoutSubviews];
self.opaque = NO;
[self.contentView addSubview:firstImageView];
[self.contentView addSubview:self.secondImageView];
[self.contentView addSubview:self.thirdImageView];
}
@end
【问题讨论】:
-
找出这段代码不起作用的原因。图像视图中的图像将在 TableView 的 cellForRowAtIndexPath 方法中设置。所以我创建图像并将其分配给 cellForRowAtIndexPath 中的 imageViews 并将其发送到单元格。这在每个单元格显示一个图像的范围内有效。我不确定为什么它不显示其他图像。