表视图数据源不显示所有行

我是一个经验丰富的网站和Android开发人员,并正在使用Swift 2开始iPhone开发的书,教导自己在运行xCode 8.2的MacBook上开发iPhone。我遇到了将本书中的例子转换为Swift 3的问题,但是已经能够处理其中大部分。

但是桌子不太好。 我已经转换了所有可以找出的方法,但是无法将所有表格数据都显示在任何示例上。 这是我的代码:

// // RootViewController.swift // Fonts // // Created by Thomas Hehl on 12/21/16. // import UIKit class RootViewController: UITableViewController { private var familyNames: [String]! private var cellPointSize: CGFloat! private var favoritesList: FavoritesList! private static let familyCell = "FamilyName" private static let favoritesCell = "Favorites" override func viewDidLoad() { super.viewDidLoad() // Uncomment the following line to preserve selection between presentations // self.clearsSelectionOnViewWillAppear = false // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem() familyNames = (UIFont.familyNames as [String]).sorted() let preferredTableViewFont = UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline) cellPointSize = preferredTableViewFont.pointSize favoritesList = FavoritesList.sharedFavoritesList tableView.estimatedRowHeight = cellPointSize } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } 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.fontNames(forFamilyName: familyName).first return fontName != nil ? UIFont(name: fontName!, size: cellPointSize) : nil } else { return nil } } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { // return the number of sections return favoritesList.favorites.isEmpty ? 1 : 2 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return section == 0 ? familyNames.count : 1 } override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return section == 0 ? "All Font Families" : "My Favorite Fonts" } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if indexPath.section == 0 { // the font names list let cell = tableView.dequeueReusableCell(withIdentifier: RootViewController.familyCell, for: indexPath) cell.textLabel?.font = fontForDisplay(atIndexPath: indexPath as NSIndexPath) cell.textLabel?.text = familyNames[indexPath.row] cell.detailTextLabel?.text = familyNames[indexPath.row] return cell } else { //the favorites list return tableView.dequeueReusableCell(withIdentifier: RootViewController.favoritesCell, for: indexPath) } } /* // Override to support conditional editing of the table view. override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { // Return false if you do not want the specified item to be editable. return true } */ /* // Override to support editing the table view. override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { // Delete the row from the data source tableView.deleteRows(at: [indexPath], with: .fade) } else if editingStyle == .insert { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view } } */ /* // Override to support rearranging the table view. override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) { } */ /* // Override to support conditional rearranging of the table view. override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { // Return false if you do not want the item to be re-orderable. return true } */ /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ } 

不幸的是,尽pipefamilyNames包含75个条目,但该表仅显示第一个屏幕。 这是截图。

表的屏幕截图

正如你所看到的,滚动条一直在底部,但这并不是所有的字体。

[ 编辑 – 聊天后,似乎问题只是OP不知道如何滚动。 OP给我发送了实际的项目,我运行了它,它工作得很好。]

你的代码工作正常。 我简直就是把你的代码复制并粘贴到Master-Detail模板中,做了一些调整(注释掉第二部分的东西,因为你没有提供关于它的任何信息),并得到这个:

在这里输入图像说明