gpt4 book ai didi

ios - 设置 rowHeight 等于 UITableViewAutomaticDimension 不起作用

转载 作者:行者123 更新时间:2023-11-30 14:17:17 27 4
gpt4 key购买 nike

我正在使用 XCode 6.3 在 iOS 8 中构建不同字体的 TableView。首先,根据我正在阅读的书,它说鉴于 iOS8,不需要对表格行的高度进行任何操作会为您处理好这一点,所以一旦我掌握了书中的所有内容,行就应该根据其内容更新其高度,但事实并非如此。然后我尝试使用 tableView.rowHeight 并将其设置为等于 TableViewController 的 viewDidLoad 函数中的 UITableViewAutomaticDimension ,但这也不起作用。我还尝试从 Interface Builder 更改行的高度,这似乎对高度也没有任何影响。我的代码如下:

class RootViewController: UITableViewController {

private var familyNames: [String]!
private var cellPointSize: CGFloat!
private var favoritesList: FavoritesList!
private let familyCell = "FamilyName"
private let favoritesCell = "Favorites"

override func viewDidLoad() {
super.viewDidLoad()

self.tableView.rowHeight = UITableViewAutomaticDimension
familyNames = sorted(UIFont.familyNames() as! [String])
let preferredTableViewFont = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
cellPointSize = preferredTableViewFont.pointSize
favoritesList = FavoritesList.sharedFavoritesList
}

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.fontNamesForFamilyName(familyName).first as! String
return UIFont(name: fontName, size: cellPointSize)
} else {
return nil
}
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// Return the number of sections.
return favoritesList.favorites.isEmpty ? 1 : 2
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return section == 0 ? familyNames.count : 1
}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return section == 0 ? "All Font Families" : "Favorite Fonts"
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier(familyCell, forIndexPath: indexPath) as! UITableViewCell
cell.textLabel!.font = fontForDisplay(atIndexPath: indexPath)
cell.textLabel!.text = familyNames[indexPath.row]
cell.detailTextLabel!.text = familyNames[indexPath.row]
return cell
} else {
return tableView.dequeueReusableCellWithIdentifier(favoritesCell, forIndexPath: indexPath) as! UITableViewCell
}
}
}

当我在模拟器中运行它时,一切看起来都很正常,直到我一直滚动到底部并得到以下结果:

fonts app

FontFamily 单元格的属性为:样式 = 副标题,附件 = 公开指示符。

关于我做错了什么有什么想法吗?

最佳答案

您必须设置self.tableView.estimatedRowHeight

关于ios - 设置 rowHeight 等于 UITableViewAutomaticDimension 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30969716/

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