3

多数のセルを含む tableivew があり、uilabel に 3 行以上を表示させようとしています。linebreakmode と numberoflines を適切に設定しましたが、まだ 3 行以上表示されません。助言がありますか?表のセルは、文字数/行数に合わせて高さを自動的に調整しますが、テキストは 3 行で表示され、次に楕円が表示されます (セルをクリックすると、全文を表示する別のビューに移動します。

以下は、UILabel を作成して表示するために必要なコードです。

   self.commentLabel = [self newLabelWithPrimaryColor:[UIColor blackColor] selectedColor:[UIColor whiteColor] fontSize:12.0 bold:YES];
    self.commentLabel.textAlignment = UITextAlignmentLeft; // default
    self.commentLabel.lineBreakMode = UILineBreakModeWordWrap;
    self.commentLabel.numberOfLines = 0; // no limit to the number of lines 
    [myContentView addSubview:self.commentLabel];
    [self.commentLabel release];

コメント全体を表のセルに表示したいと思います。

4

2 に答える 2

0

自動レイアウトの設計コンセプトでは、UILabel に高さの制約を設定せず、no を設定します。行の 0 として

Autolayout は、ラベルのテキストに従ってラベルの動的な高さを自動的に処理します。ラベルに 1 行のテキストがある場合は、1 行のスペースのみを占有します。また、ラベルに複数の行がある場合、テキストのサイズとテキストの表示に必要な行数に応じてラベルのサイズが変更されます。

  • tableview dataSource とデリゲートの割り当てと実装
  • UITableViewAutomaticDimensionrowHeight と EstimatedRowHeight に割り当てます
  • デリゲート/データソース メソッドを実装する (つまりheightForRowAt、それに値を返すUITableViewAutomaticDimension)

-

目標 C:

// in ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

  @property IBOutlet UITableView * table;

@end

// in ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    self.table.dataSource = self;
    self.table.delegate = self;

    self.table.rowHeight = UITableViewAutomaticDimension;
    self.table.estimatedRowHeight = UITableViewAutomaticDimension;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return UITableViewAutomaticDimension;
}

この回答を見てください: UITableViewCell内でUILabelを動的に調整しますか?

于 2017-06-21T12:07:41.220 に答える