0

I'm using XCode 6.3 to build a TableView of the different Fonts in iOS 8. First, per the book I'm reading, it said that nothing needed to be done regarding the height of the table rows, given that iOS8 takes care of that for you, so once I had everything per the book, the rows should update their heights based on their content, which wasn't the case. Then I tried to play with tableView.rowHeight and I set it equal to UITableViewAutomaticDimension in the TableViewController's viewDidLoad function, and that didn't work either. I also tried changing the height of the rows from Interface Builder, and that didn't seem to have any effect on the heights either. My code is as follows:

class RootViewController: UITableViewController {

    private var familyNames: [String]!
    private var cellPointSize: CGFloat!
    private var favoritesList: FavoritesList!
    private let familyCell = "FamilyName"
    private let favoritesCell = "Favorites"

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.rowHeight = UITableViewAutomaticDimension
        familyNames = sorted(UIFont.familyNames() as! [String])
        let preferredTableViewFont = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
        cellPointSize = preferredTableViewFont.pointSize
        favoritesList = FavoritesList.sharedFavoritesList
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        tableView.reloadData()
    }

    func fontForDisplay(atIndexPath indexPath: NSIndexPath) -> UIFont? {
        if indexPath.section == 0 {
            let familyName = familyNames[indexPath.row]
            let fontName = UIFont.fontNamesForFamilyName(familyName).first as! String
            return UIFont(name: fontName, size: cellPointSize)
        } else {
            return nil
        }
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // Return the number of sections.
        return favoritesList.favorites.isEmpty ? 1 : 2
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Return the number of rows in the section.
        return section == 0 ? familyNames.count : 1
    }

    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return section == 0 ? "All Font Families" : "Favorite Fonts"
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCellWithIdentifier(familyCell, forIndexPath: indexPath) as! UITableViewCell
            cell.textLabel!.font = fontForDisplay(atIndexPath: indexPath)
            cell.textLabel!.text = familyNames[indexPath.row]
            cell.detailTextLabel!.text = familyNames[indexPath.row]
            return cell
        } else {
            return tableView.dequeueReusableCellWithIdentifier(favoritesCell, forIndexPath: indexPath) as! UITableViewCell
        }
    }
}

When I run this in the simulator, everything looks right until I scroll all the way to the bottom and I get this:

fonts app

The attributes of the FontFamily cell are: style = subtitle, and accessory = disclosure indicator.

Any ideas on what I'm be doing wrong?

modsoussi
  • 137
  • 11
  • 2
    In order to use self-sizing cells you need to set BOTH the 'rowHeight' and 'estimatedRowHeight'. Set the tableView.estimatedRowHeight = 44 (or some close approximate value), and it will work – DBoyer Jun 21 '15 at 22:11
  • That was it! Thanks a lot! – modsoussi Jun 21 '15 at 22:14
  • You don't need to set either. You just need to not implement the `heightForRow` method... – nhgrif Jun 22 '15 at 00:44
  • I had it that way, and it wasn't working. Once I set r`owHeight` and `estimatedRowHeight`, everything turned out right. – modsoussi Jun 22 '15 at 17:57
  • @nhgrif Self-sizing table view cells leverage the fact they they can use the estimated height to defer the height calculation, so it doesn't work unless you specify both – DBoyer Jun 23 '15 at 22:34
  • Except in the last few projects I've done, specified neither, and it worked perfectly fine... – nhgrif Jun 23 '15 at 22:44

1 Answers1

0

You must set self.tableView.estimatedRowHeight

ILYA2606
  • 587
  • 3
  • 7