私は自分の TableViewCell をカスタマイズしたいのですが、1 つの画像で満たすだけです。そして、セルとイメージビューの両方のフレームのサイズを変更しようとしましたが、このプログラムを実行すると、画面にイメージが表示されません。
顧客セルに関する私のコードは次のとおりです。
- (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で、2番目のセルをクリックすると画像が表示されますが、最初のセルの最初の画像はまだ表示されません。何らかの関係がありますか?