My Locations Tutorials pg. 110: Address Cell Size Problem

After tagging location, the address does not seem to fit in the address cell properly as pictured below. I’m wondering where the problem is coming from. Either the code or in main storyboard?

//
//  LocationDetailsViewController.swift
//  JMyLocations
//
//  Created by James Sison on 8/4/16.
//  Copyright © 2016 Big Nerd Ranch. All rights reserved.
//

import UIKit
import CoreLocation

private let dateFormatter: NSDateFormatter = {
    let formatter = NSDateFormatter()
    formatter.dateStyle = .MediumStyle
    formatter.timeStyle = .ShortStyle
    print()
    return formatter
}()

class LocationDetailsViewController: UITableViewController {
    
    var coordinate = CLLocationCoordinate2D(latitude: 0, longitude: 0)
    var placemark: CLPlacemark?
    
    @IBOutlet weak var descriptionTextView: UITextView!
    @IBOutlet weak var categoryLabel: UILabel!
    @IBOutlet weak var latitudeLabel: UILabel!
    @IBOutlet weak var longitudeLabel: UILabel!
    @IBOutlet weak var addressLabel: UILabel!
    @IBOutlet weak var dateLabel: UILabel!
    
    @IBAction func done() {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    @IBAction func cancel() {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        descriptionTextView.text = ""
        categoryLabel.text = ""
        
        latitudeLabel.text = String(format: "%.8f", coordinate.latitude)
        longitudeLabel.text = String(format: "%.8f", coordinate.longitude)
        
        if let placemark = placemark {
            addressLabel.text = stringFromPlacemark(placemark)
        } else {
            addressLabel.text = "No Address Found"
        }
        
        dateLabel.text = formatDate(NSDate())
    }
    
    func stringFromPlacemark(placemark: CLPlacemark) -> String {
        var text = ""
        
        if let s = placemark.subThoroughfare {
            text += s + " "
        }
        if let s = placemark.thoroughfare {
            text += s + ", "
        }
        if let s = placemark.locality {
            text += s + ", "
        }
        if let s = placemark.administrativeArea {
            text += s + " "
        }
        if let s = placemark.postalCode {
            text += s + ", "
        }
        if let s = placemark.country {
            text += s
        }
        return text
    }

    
    func formatDate(date: NSDate) -> String {
        return dateFormatter.stringFromDate(date)
    }
    
    // Mark: - UITableViewDelegate
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if indexPath.section == 0 && indexPath.row == 0 {
            return 88
            
        } else if indexPath.section == 2 && indexPath.row == 2 {
            addressLabel.frame.size = CGSize(width: view.bounds.size.width - 115, height: 10000)
            addressLabel.sizeToFit()
            addressLabel.frame.origin.x = view.bounds.size.width - addressLabel.frame.size.width - 15
            return addressLabel.frame.size.height + 20
            
        } else {
            return 44
        }
    }
}

hieghtForRowAtIndexPath is else for indexPath.section == 2 && indexPath.row == 2. The frame doesnt seem to be updating. Try harcoding a value first to make sure it does get changed. Try 44, then try addressLabel.frame.size.height + 44.

How do you hardcode the value?

I tried changing the addressLabel.frame.size.height to + 44, and it updated correctly.

You hardcode a height value for a cell through the “size inspector” section of the storyboard. Select the cell in question and change the height in the “size inspector” accordingly. Furthermore, here is a code for the cell heights:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath.section == 0 && indexPath.row == 0 {
        return 88
    } else if indexPath.section == 2 && indexPath.row == 2 {
        addressLabel.frame.size = CGSize(width: view.bounds.size.width - 115, height: 10000)
        addressLabel.sizeToFit()
        addressLabel.frame.origin.x = view.bounds.width - addressLabel.frame.size.width - 15
        return addressLabel.frame.size.height + 20
    } else if indexPath.section == 1 {
        if imageView.hidden{
            return 44
        }else{
            return 280
        }
    }else{
        return 44
    }
}

there is an error on an unresolved identifier, imageView.hidden

The reason why you have an unresolved identifier error is because you have not declared an outlet variable in your class. Add the following code inside your location detail view controller:

@IBOutlet weak var imageView: UIImageView!

Then in the storyboard go to the location detail view controller window and control drag from the yellow icon on top to the UIImageView object and choose imageView. This makes a connection to your new imageView outlet variable. Remember to get rid of any prior outlet connections you have to the UIImageView.