我想自定义我的 TableViewCell,它只填充一张图像。我试图调整单元格和我的图像视图的框架,但是当我运行这个程序时,我在屏幕上看不到我的图像。
这是我关于客户单元的代码:
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self initLayout];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)initLayout {
self.imageView.frame = self.contentView.frame;
[self addSubview:_image];
}
- (void)resizeWithWidth:(NSInteger)width {
// I want the picture's width equal to the screen's, and the picture's height is 1/3 of its width
CGRect frame = [self frame];
frame.size.width = width;
frame.size.height = width / 3;
self.frame = frame;
self.imageView.frame = frame;
}
在我的 TableViewController 中,我以这种方式获取单元格:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
IntroductViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"introductcell" forIndexPath:indexPath];
// Configure the cell...
if (cell) {
cell = [[IntroductViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"introductcell"];
}
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
if (indexPath.row == 0) {
cell.imageView.image = [UIImage imageNamed:@"1.jpeg"];
} else if (indexPath.row == 1) {
cell.imageView.image = [UIImage imageNamed:@"2.jpeg"];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell resizeWithWidth:[UIScreen mainScreen].bounds.size.width];
return cell;
}
而且,这是我在运行程序时看到的:
PS:我注意到每个单元格都有一些空间。我怎样才能删除它们?
PS 2:我试图删除该行: cell.selectionStyle = UITableViewCellSelectionStyleNone; 在cellForIndex中,我发现点击第二个单元格时,它显示了图像,但我仍然看不到第一个单元格中的第一个图像,有什么关系吗?