I was writing an iOS app in Swift. I created a table view scene, but it is only displaying its first subview. I tried to make the code as simple as possible. I created a table view cell with two labels, but it still only displays the first label. The code is as follows. Why isn't the second label displayed? Are my AutoLayout constraints wrong? Thanks!
import UIKit
class TestCell: UITableViewCell {
let viewsDict: [String : UILabel] = [
"first": UILabel(),
"second": UILabel(),
]
override func awakeFromNib() {
viewsDict["first"]!.text = "first"
viewsDict["second"]!.text = "second"
viewsDict["first"]!.translatesAutoresizingMaskIntoConstraints = false
viewsDict["second"]!.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(viewsDict["first"]!)
contentView.addSubview(viewsDict["second"]!)
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[first]-[second]-|", options: [], metrics: nil, views: viewsDict))
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[first]", options: [], metrics: nil, views: viewsDict))
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[second]", options: [], metrics: nil, views: viewsDict))
}
}
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
}
}