0

タイトルとテキストで構成される「ストーリー」をユーザーが作成できるようにするアプリケーションを構築しています。テーブルビュープロパティに割り当てられた文字列が長すぎるとセルが表示されない

作成したすべてのストーリーを表示するtableViewを実装しています。これまでのところすべてが機能します。

ユーザがというタイトルまたはテキストを入力すると、tableViewCellが表示できるものより長く、そのセルはすべてに表示されません。 名前の短いものもやっています。

私はセルスタイル "字幕"を使用しています。

セルに表示されるテキストの量を制限する方法と、このバグの原因を教えてください。私はそれを修正する方法を見つけたとしても、おそらくスクリーンから出ているテキストにはまだ問題があるでしょう。ここで

は私のUITableViewControllerクラスのコードです:あなたは高さ可変TableViewCellを使用する必要があることについては

Here is a screenshot of the UI from the interface builder

+0

したがって、ユーザーは、物語の1000個の単語を入力すると、あなたはその特定のセル内の各単語を表示したい場合は? –

+0

**理想的には最初のx文字のみ** しかし、テキストが長すぎると、現在私のtableViewに何も表示されません。 – Marmelador

+2

uiのスクリーンショットを追加します。 –

答えて

0

:ここ

class StoryTableViewController: UITableViewController { 


override func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return savedStories.count 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) 

    cell.textLabel?.text = savedStories[indexPath.row].title 
    cell.detailTextLabel?.text = savedStories[indexPath.row].text 
    return cell 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Uncomment the following line to preserve selection between presentations 
    // self.clearsSelectionOnViewWillAppear = false 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

は、インタフェースビルダーからUIのスクリーンショットです

override func viewDidLoad() 
{ 
    super.viewDidLoad() 

    self.tableView.estimatedRowHeight = 200 // give maximum height you required 
    self.tableView.rowHeight = UITableViewAutomaticDimension 
} 

このデリゲートメソッドはビューコントローラにあります

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
{ 
    return UITableViewAutomaticDimension 
} 
1

カスタムUITableViewCellを作成する必要があります。また、セルをテキスト長に自動的に調整するために、使用可能な動的なサイズ変更可能なセルを使用できます。

IBステップ: セルにUILabelを作成します。高さの制約を与えないでください。ちょうどすべての側面からそれをピンと以下の操作を行いますのviewDidLoadで

label.numberOfLines = 0 

self.tableView.estimatedRowHeight = 88.0 //Any estimated Height 
self.tableView.rowHeight = UITableViewAutomaticDimension 

heightForRow:方法を記述しませんがしますので、そこに既存の複数のセルのそれを使用したい場合、その特定のセルの高さに対してUITableViewAutomaticDimensionを返すことができます。

あなたはこれら二つのデリゲートを実装する必要があり、この1 out

1

を試してみて、VCでのtableViewデリゲートとデータソースをバインドすることを忘れないでください、あなたはストーリーボードから説明プロパティnumberOfLines = 0にラベルを設定します。ストーリーボードで今

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 60; // height of default cell 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return UITableViewAutomaticDimension; // It takes automatic height of cell 
} 

この

下のあなたのビュー階層は、この

enter image description here

チェックのみViewLabelContatainer

ビューを追加し、にすべてのラベルを置くようにする必要がありますそれ。コンテナが

enter image description here

ラベルのタイトル制約

enter image description here

ラベル説明制約

をcontraints

ラベル

enter image description here

出力

enter image description here

関連する問題