gpt4 book ai didi

ios - TableView 数据源未显示所有行

转载 作者:行者123 更新时间:2023-11-28 08:21:00 26 4
gpt4 key购买 nike

我是一名经验丰富的 Web 和 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.
}
*/

}

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

Screenshot of Table

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

最佳答案

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

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

enter image description here

关于ios - TableView 数据源未显示所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41293107/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com