I wanna display two UILabels(Title, Subtitle) in a cell.
for Title: multiple line > 0
for Subtitle: 0 ≤ multiple line ≤ 2
A bug here(say, title&subtitle are both 2 lines, cell's indexPath.row = 1):
The first time I run the simulator, title only shows 1 line and subtitle shows 2 lines. After I scrolled the tableView and back to the first indexPath.row, it display correctly! It seems like this:
First time:
—————————————————
This is the tile, and it should display...
This is the subtitle, and it should show
2 rows.
—————————————————
After scroll and back to this cell:
—————————————————
This is the title, and it should display
two rows.
This is the subtitle, and it should show
2 rows.
—————————————————
What I've done:
In controller: tableView.estimatdRowHeight = 79
tableView.rowHeight = UITableViewAutomaticDimension
In storyboard:
For title:
- Leading space to superview
- Trailing space to superview
- Top space to Superview
- Align leading to Subtitle
- Align trailing to Subtitle
- Bottom space to Subtitle
For subtitle:
- Bottom space to superview
- Align Leading to Title
- Align trailing to Title
- Top space to Title
I'm confused for this bug several days, any ideas to solve this? Big thanks!!!(Sorry for lacking image cuz I don't get enough reputation ><)
Cells that are loaded from a storyboard don't start out with the right initial width.
This is why the label isn't sized properly the first time, but appears correctly once you (reload the data, or) scroll the cell off-screen, then on-screen.
Since the cell width is initially incorrect, the label ends up using a
preferredMaxLayoutWidth
which is wider than the table. (The label thought it had more room to fit everything on one line.)The solution which worked for me was to make sure my (subclassed) cell's width matched the tableView's width:
In TableViewCell.m:
I'd also recommend checking out smileyborg's excellent answer about self-sizing cells, along with his sample code. It's what tipped me off to the solution, when I bumped into the same issue you are having.