1

独自の XIB を持つカスタム UITableViewCell を作成しようとしています。これには、2 つの重要な編集可能なコンポーネントが含まれています。

  • UIラベル
  • 'end-cap insets' を持つ UIImage - この画像はラベルの後ろにあり、それに合わせてサイズ変更されます

UITableViewCell を作成すると、そのラベルのテキストを簡単に設定できますが、同じ方法で、UIImage のフレームを UILabel のフレームと一致するように変更しようとします。ただし、テーブルビューをスクロールしてセルを「リロード」してデキューし、表示に戻すまで、これは発生しません。

ビュー コントローラーの .m にカスタム テーブル ビュー セルを作成します。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *UITableViewCellIdentifier = @"msgCell";

    RMMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:UITableViewCellIdentifier];

    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MessageCell" owner:self options:nil];
        cell = [topLevelObjects objectAtIndex:0];
        [cell customInit]; //This sets up the ImageView's image etc
    }

    [cell setMessageValue:[_messages objectAtIndex:indexPath.row]]; //SEE BELOW

    return cell;
}

...そして、カスタム セルの .m では、テキスト ラベルの設定と画像のサイズ変更を次の方法で処理します。

- (void)setMessageValue:(NSString *)message {

    _testLabel.text = message;
    // Assume the text label's frame has already been set to the message's length 
    // (will add this in once I figure out how to change the imageView's 
    // frame correctly)

    // Set the image view's frame to the label's
    // THIS DOES NOT TAKE EFFECT UNTIL THE TABLE VIEW IS RE-SCROLLED
    CGRect frame = _imageView.frame;
    frame.size.width = _testLabel.frame.size.width + 8;
    _imageView.frame = frame;
}
4

3 に答える 3