0

我正在構建一個應用程序,讓用戶創建一個包含標題和文本的「故事」。表格單元格不顯示分配給屬性的字符串是否太長

我正在實現一個顯示所有創建的故事的tableView。到目前爲止,一切正常但這裏是我的問題:

當用戶輸入一個標題或文本的長度是什麼tableViewCell將能夠顯示,該單元根本不顯示。 儘管如此,其他人名字較短。

我正在使用單元格樣式「副標題」。

如何限制顯示在單元格中的文本數量以及導致此錯誤的原因?因爲即使我找到了修復方法,但屏幕上的文字仍然可能會出現問題。

這裏是我的UITableViewController類代碼:

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的從界面生成器的截圖:

Here is a screenshot of the UI from the interface builder

+0

因此,如果用戶輸入1000個字的故事,你想顯示在那個特定的細胞每一個字? –

+0

**理想情況下,只有前x個字符。** 但我的tableView目前不顯示任何內容,當文本太長。 – Marmelador

+2

添加UI的屏幕截圖。 –

回答

0

對於您需要使用可變高度TableViewCell

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。不要給它任何高度限制。剛剛從四面八方腳起來,做如下:

label.numberOfLines = 0 

在viewDidLoad中:

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

不要寫heightForRow:方法,但如果你想使用它,因爲現有的幾個有細胞的,你可以爲該特定單元格高度返回UITableViewAutomaticDimension。

試試這個out

1

您必須實現這兩個代表,不要忘記的tableView委託和數據源綁定VC,並設置你的故事板標籤說明財產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

標籤說明約束

enter image description here

輸出

enter image description here

相關問題