0

I am building an app that lets the user create a "Story" which consists of a title and a text.

I am implementing a tableView that shows all created stories. So far everything works. But here is my issue:

When the user enters a title or text that is longer that what tableViewCell would be able to display, that cell doesn't show up at all. Others with shorter names still do though.

I am using the cell style "subtitle".

How does one go about limiting the amount of text showing in the cell and what causes this bug? Because even if I find a way to fix it, there will probably still be a problem with text running off the screen.

Here is the code in my UITableViewController class:

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.
}

Here is a screenshot of the UI from the interface builder:

Here is a screenshot of the UI from the interface builder

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
Marmelador
  • 947
  • 7
  • 27

3 Answers3

1

You need to create your custom UITableViewCell. And you can use available dynamic resizable cells for adjusting cell automatically to text length.

IB steps: Make a UILabel on cell. Don't give any height constraint to it. just pin it up from all sides and do the followings :

label.numberOfLines = 0

In viewDidLoad:

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

Don't write heightForRow: method but if you want to use it because of several cells existing there , you can return UITableViewAutomaticDimension for that particular cell height.

Try this one out

ankit
  • 3,537
  • 1
  • 16
  • 32
1

You have to implement these two delegates, don't forget to bind tableView delegate and datasource with VC and set you label description property numberOfLines = 0 from storyboard.

- (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
}

Now in storyboard do this below

Your view hierarchy should be like this

enter image description here

check only ViewLabelContatainer

Add a view and put all labels into it.

Label Container contraints

enter image description here

Label Title constraint

enter image description here

Label Description constraint

enter image description here

output

enter image description here

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
0

For that you need to use variable height TableViewCell

override func viewDidLoad() 
{  
    super.viewDidLoad()

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

then add this delegate methode in your view controller

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
{
    return UITableViewAutomaticDimension
}
Ganesh Manickam
  • 2,113
  • 3
  • 20
  • 28