0

私は自分の 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番目のセルをクリックすると画像が表示されますが、最初のセルの最初の画像はまだ表示されません。何らかの関係がありますか?

4

2 に答える 2

0

コードにいくつか問題がある可能性がありますが、コンテキストを追加せずに問題が解決するかどうかは保証できません。

1) - dequeueReusableCellWithIdentifier:forIndexPath: を使用しています。これには、registerNib:forCellReuseIdentifier: または registerClass:forCellReuseIdentifier: のいずれかを呼び出す必要があります (インターフェイス ビルダーを使用している場合は 1 つ目、コードのみでセルを定義した場合は 2 つ目)。あなたの場合、2番目のものが必要になると思います。例 (これは、TableViewController の viewDidLoad に適しています):

[tableView registerClass: [IntroductViewCell class] forCellReuseIdentifier: @"introductcell"];

私のObjective-Cが少しずれていたらすみません。私は昨年かそこらの間Swiftを独占的に使用しています。すでにこれを持っていて、その部分をこの質問から除外した場合は、それで問題ありません。

2) このビットを取り除く:

   if (cell) {
       cell = [[IntroductViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"introductcell"];
   }

まず第一に、おそらく !cell (またはそのようなもの) が必要でした。おそらく、既にそこにあるセルを破棄したくありませんでした (割り当てられていない場合は、新しいセルが必要ですよね? セルの取得が成功した場合に再割り当てする必要はありません)。第 2 に、registerClass / dequeueReusableCellWithIdentifier:forIndexPath: コンボを実行すると、不要になります (特に、後者のメソッドは常に有効なセルを返します)。

于 2016-07-08T17:42:08.650 に答える
-1

区切り線を削除するには、tableView.separatorStyle = UITableViewCellSeparatorStyleNone; を使用します。

于 2016-07-08T16:19:05.107 に答える