9

カスタム UITableViewCell を作成せずに、iOS 8 で UITableViewCell をセルフサイズにすることは可能ですか?

標準の UITableViewCell タイプ (UITableViewCellStyleDefault、UITableViewCellStyleSubtitle、UITableViewCellStyleValue1、UITableViewCellStyleValue2) には自動レイアウト制約が組み込まれていると思いました。これは、ストーリーボードで非カスタム セルの制約を変更できないという事実によって確認されます。

しかし、タイプ UITableViewCellStyleValue1 の非カスタム セルを使用し、それを Storyboard で設定し、textLabel と detailTextLabel の numberOfRows を 0 に設定し、viewDidLoad コードを以下のように設定すると、セルの textLabel のみが自動サイズ調整で考慮されます。セルの高さ。detailTextLabel が textLabel よりも多くの行に表示される場合、detailTextLabel のテキストはセルの上端と下端にはみ出します。繰り返しますが、セルは textLabel に対して適切にサイズ変更されますが、サイズ変更プロセスで detailTextLabel を無視するようです。

私が知る必要がある主なことは、動的テキストと自己サイジングを適切にサポートしたい場合、標準セルを使用できる行に対してもカスタムセルを作成する必要があるかということです.

- (void)viewDidLoad {

    [super viewDidLoad];

    [self.tableView setEstimatedRowHeight:DEFAULT_ROW_HEIGHT];
    [self.tableView setRowHeight:UITableViewAutomaticDimension];
}
4

1 に答える 1

4

iOS 10/XCode 8 (iOS 9/XCode 7 で同じ結果) で異なるセル タイプを使用してこれを試したところ、detailTextLabel ではなく textLabel でのみ可能であるように見えます。

(基本的に、OPが言及した問題を繰り返します)

detailTextLabel と textLabel にテキストを交互に設定する ViewController コード。

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.estimatedRowHeight = 44
        tableView.rowHeight = UITableViewAutomaticDimension
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        if indexPath.row % 2 == 0 {
            cell.textLabel?.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
            cell.detailTextLabel?.text = "<- textLabel"
        } else {
            cell.textLabel?.text = "detailTextLabel ->"
            cell.detailTextLabel?.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
        }
        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

}

textLabel と textDetailLabel の line プロパティを 0 に設定してください。結果は次のとおりです。

基本セル ここに画像の説明を入力

右詳細セル ここに画像の説明を入力

左詳細セル ここに画像の説明を入力

字幕セル ここに画像の説明を入力

これをバグとして報告します。

于 2016-07-18T21:56:06.470 に答える