2017-07-05 14 views
0

私はiOS8セルフサイジングセルを使用しています。セルのサイズをsubview,messageBodyButtonprofileImageViewに合わせたいとします。
profileImageViewのサイズは固定です。下の図に示すように、messageBodyButtonは、titleLabelに合わせてサイズが変更されません。UIButtonのタイトルに合わせてサイズが変更されていません

どうしてですか?自動レイアウトを使ってこのバグを修正するには?
titleLabelの内容が変更されたとき(モデルのデータが変更されたなど)、セルの高さを正しく計算できるように制約を更新する方法はありますか?ここで

pic

私のコードです:

RXTChatCell.m

@property (nonatomic, strong) UILabel *timeLabel; 
@property (nonatomic, strong) UIImageView *profileImageView; 
@property (nonatomic, strong) UIButton *messageBodyButton; 

@property (nonatomic, assign) BOOL needUpdateConstraints; 
@property (nonatomic, assign) BOOL didSetUpConstraints; 

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     _timeLabel = [[UILabel alloc] init]; 
     _timeLabel.text = @"19:00"; 
     [self.contentView addSubview:_timeLabel]; 

     _profileImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"placeholder"]]; 
     [self.contentView addSubview:_profileImageView]; 

     _messageBodyButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [_messageBodyButton.titleLabel setLineBreakMode:NSLineBreakByTruncatingTail]; 
     [_messageBodyButton.titleLabel setTextAlignment:NSTextAlignmentRight]; 
     _messageBodyButton.titleLabel.numberOfLines = 0; 
     _messageBodyButton.titleLabel.backgroundColor = UIColor.grayColor; 
     [self.contentView addSubview:_messageBodyButton]; 

     _timeLabel.backgroundColor = UIColor.redColor; 
     _profileImageView.backgroundColor = UIColor.greenColor; 
     _messageBodyButton.backgroundColor = UIColor.blueColor; 
     self.contentView.backgroundColor = UIColor.orangeColor; 
    } 
    return self; 
} 

- (void)updateConstraints 
{ 
    if (!self.didSetUpConstraints) { // set up constraints 
     [_timeLabel makeConstraints:^(MASConstraintMaker *make) { 
      make.top.equalTo(self.contentView.topMargin); 
      make.centerX.equalTo(self.contentView); 
     }]; 

     [_profileImageView makeConstraints:^(MASConstraintMaker *make) { 
      make.top.equalTo(_timeLabel.bottom).offset(10); 
      make.right.equalTo(self.contentView).offset(-10); 
      make.width.and.height.equalTo(50); 
      make.bottom.lessThanOrEqualTo(self.contentView.bottomMargin); 
     }]; 

     [_messageBodyButton makeConstraints:^(MASConstraintMaker *make) { 
      make.top.equalTo(_profileImageView); 
      make.right.equalTo(_profileImageView.left).offset(-10); 
      make.left.equalTo(self.contentView).offset(10); 
      make.bottom.lessThanOrEqualTo(self.contentView.bottomMargin); 
     }]; 
     self.didSetUpConstraints = YES; 
    } 

    if (self.needUpdateConstraints){ 
     // here, how should I update constraints? 

    } 

    [super updateConstraints]; 
} 

- (void)setMessage:(RXTChatMessage *)message 
{ 
    EMTextMessageBody *body = (EMTextMessageBody *)message.messageBody.body; 
    [self.messageBodyButton setTitle:body.text forState:UIControlStateNormal]; 
    self.needUpdateConstraints = YES; 
    [self setNeedsUpdateConstraints]; 
} 

RXTChatViewController.m

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 
    self.tableView.estimatedRowHeight = 44; 
    self.tableView.rowHeight = UITableViewAutomaticDimension; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    RXTChatMessage *message = self.messages[indexPath.row]; 
    RXTChatCell *cell = [tableView dequeueReusableCellWithIdentifier:RXTChatCellId]; 
    if (cell == nil) { 
     cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:RXTChatCellId]; 
    } 
    cell.message = message; 

    return cell; 
} 
+3

あなたのコードを表示する - >助けを得る - >勝利は – quinz

+0

'CODE' –

+0

はまた、あなたがあなたのセルのサブビューのために適用される制約を表示して質問を更新してください。 – D4ttatraya

答えて

0
  1. あなたのボタンを選択 - >エディタに行くと - >内容に合わせたサイズ。
  2. ボタンの固定高さの制約を解除します(スーパービューのみをセルビューにする必要があります)。
  3. これはあなたの問題を解決するかどうか私に教えて

あなたの問題を解決する必要があります。

]

+0

申し訳ありませんが、私はInterface Builderで作業していないので、 'layoutSubviews'に' [self.messageBodyBody sizeToFit];を追加しようとしましたが動作しませんでした。 – PeteRuan

+0

@PeteRuan:うん、あなたはそれを使い始める方がいいか、すべてのあなたのアプリはこのように見えるだろう)PS。あなたはどんな種類の制約も提供しませんでした。ある夜を過ごし、IBでそれらを使用することを今すぐ学ぶことをお勧めします。これは、u planeがiosアプリで再び動作する場合、次の数ヶ月間でx100の要素で返済されます。 –

+0

@Umka:ありがとうございました。私はIB asapの使用を開始します。 – PeteRuan

関連する問題