表视图数据源未显示所有行 [英] Table View Data Source not showing all the rows

查看:21
本文介绍了表视图数据源未显示所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名经验丰富的 Web 和 Android 开发人员,正在使用 Swift 2 开始 iPhone 开发一书在运行 xCode 8.2 的 MacBook 上自学 iPhone 开发.我在将书中的示例转换为 Swift 3 时遇到了问题,但有能够处理其中的大部分.

I am a seasoned web and Android developer and am teaching myself iPhone development on my MacBook running xCode 8.2 using the book Beginning iPhone Development with Swift 2. I am running into issues converting the examples in the book to Swift 3, but have been able to deal with most of them.

然而,桌子进展得并不顺利.我已经转换了我能找到的所有方法,但是我无法在任何示例中显示所有表数据.这是我的代码:

The tables are not going well, however. I have converted all of the methods I can figure out, but I can't get all of the table data to show on any of the examples. Here is my code:

//
//  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.
    }
    */

}

不幸的是,即使 familyNames 包含 75 个条目,该表也只显示了第一个屏幕.这是屏幕截图.

Unfortunately, even though that familyNames contains 75 entries, the table is only showing the first screenful. Here is the screenshot.

如您所见,滚动条一直在底部,但这几乎不是所有字体.

As you can see, the scrollbar's all the way at the bottom, but that's not nearly all of the fonts.

推荐答案

[EDIT - 聊天后,问题似乎只是 OP 不知道如何滚动.OP 向我发送了实际项目,我运行了它,并且运行良好.]

[EDIT - After chat, it appears the problem is merely that the OP doesn't know how to scroll. The OP sent me the actual project, I ran it, and it works just fine.]

您的代码运行良好.我实际上只是将您的代码复制并粘贴到主从模板中,进行了一些调整(注释掉第二部分的内容,因为您没有提供任何相关信息),然后得到:

Your code works fine. I literally just copied and pasted your code into the Master-Detail template, made a few adjustments (commenting out the second section stuff because you didn't supply any info about it), and got this:

这篇关于表视图数据源未显示所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆