【问题标题】:Setting rowHeight equal to UITableViewAutomaticDimension not working设置 rowHeight 等于 UITableViewAutomaticDimension 不起作用
【发布时间】:2015-06-21 21:56:01
【问题描述】:

我正在使用 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
        }
    }
}

当我在模拟器中运行它时,一切看起来都是正确的,直到我一直滚动到底部并得到以下信息:

FontFamily 单元格的属性是:style = 副标题,accessory = 披露指示符。

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

【问题讨论】:

  • 为了使用自动调整大小的单元格,您需要同时设置“rowHeight”和“estimatedRowHeight”。设置 tableView.estimatedRowHeight = 44(或某个接近的近似值),它将起作用
  • 就是这样!非常感谢!
  • 你也不需要设置。您只需要不实现heightForRow 方法...
  • 我就是这样的,但它不起作用。一旦我设置了 rowHeightestimatedRowHeight,一切都变得正确。
  • @nhgrif 自我调整大小的表格视图单元格利用了他们可以使用估计高度来推迟高度计算的事实,因此除非您同时指定两者,否则它不起作用

标签: ios swift uitableview xcode6 xcode6.3.2


【解决方案1】:

您必须设置 self.tableView.estimatedRowHeight

【讨论】:

    猜你喜欢
    • 2018-06-12
    • 2014-11-11
    • 1970-01-01
    • 2019-04-19
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 2014-10-27
    • 2016-04-07
    相关资源
    最近更新 更多