Reputation: 1026
I tried this code and crashed it says unwrapped it. so how I can show font with display and name on left cells.
result code:
import UIKit
class ViewController: UIViewController {
let names = UIFont.familyNames.sorted()
@IBOutlet weak var FontTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
then i add the code to set display font with name. then it was crash this one.
}
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
names.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cells = FontTableView.dequeueReusableCell(withIdentifier: "Cells")
for family in UIFont.familyNames.sorted() {
let names = UIFont.fontNames(forFamilyName: family)
cells?.textLabel?.text = ("Family: \(family)")
cells?.detailTextLabel?.text = ("Family: \(names)")
}
return cells!
}
}
Upvotes: 0
Views: 378
Reputation: 5052
I don't know what are you going to show in a single row. In your code cellForRowAt, every single row will run the loop. I guess you should not use the loop, instead use the data source names. Also, fontNames is also an array of Strings. So, based on your requirement(1st one may be), assign to detailsLabel.
let family = names[indexPath.row]
let fontNames = UIFont.fontNames(forFamilyName: family)
cells?.textLabel?.text = ("Family: \(family)")
cells?.detailTextLabel?.text = ("Family: \(fontNames)")
Upvotes: 1