【问题标题】:UIView as a header for UITableViewUIView 作为 UITableView 的标头
【发布时间】:2020-07-02 23:39:33
【问题描述】:

我正在通过《iOS 编程:大书呆子牧场指南(第 6 版)》这本书快速学习 iOS 编程,但它似乎有点过时了。我在 Storyboard 中向 UIViewTable 添加标题时遇到了问题。 在书中,它建议在原型单元格顶部添加一个 UIView,将带有约束的按钮放在那里,仅此而已,但就我而言,这不起作用。按钮不会显示:( 所以,它看起来像这样:

请帮我看看这个例子好吗?

我的 ItemsViewController.swift:

import UIKit

class ItemsViewController: UITableViewController {

    var itemStore: ItemStore!

    @IBAction func addNewItem(_ sender: UIButton) {

    }

    @IBAction func toggleEditingMode(_ sender: UIButton) {

        if isEditing {
            sender.setTitle("Edit", for: .normal)
            setEditing(false, animated: true)
        } else {
            sender.setTitle("Done", for: .normal)
            setEditing(true, animated: true)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
    }

    override func numberOfSections(in tableView: UITableView) -> Int {

        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return itemStore.allItems.count + 1
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)

        if itemStore.allItems.count == indexPath.row {

            cell.textLabel?.text = "No more items"
        } else {

            let item = itemStore.allItems[indexPath.row]
            cell.textLabel?.text = item.name
            cell.detailTextLabel?.text = "$\(item.valueInDollars)"
        }

        return cell
    }
}

UPD:我已经通过tableView(_:viewForHeaderInSection:) 以编程方式完成了这项工作,但这并不方便。 有没有什么办法可以达到和storyboard方式一样的效果?

【问题讨论】:

  • 能不能加“tableView.tableHeaderView”或者表头高度并检查一下。
  • 请发布您的ItemsViewController.swift 是什么样的,因为我尝试了与您相同的东西,并且顶部的视图在我的模拟器中正确显示
  • 当然,我已将我的 ItemsViewController.swift 添加到帖子中。

标签: ios swift uitableview uiview


【解决方案1】:

可能与headerViewSize 大小有关。请尝试以下方法。

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    guard let tableHeaderView = tableView.tableHeaderView else { return }
    let size = tableHeaderView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
    if tableHeaderView.frame.size.height != size.height {
        tableHeaderView.frame.size.height = size.height
    }
    tableView.tableHeaderView = tableHeaderView
}

【讨论】:

  • 不工作:(如果我理解正确,这段代码取决于设置 tableHeaderView 属性,我没有设置。正确的方法是什么?我试图为我的UIVIew 然后 tableView.tableHeaderView = headerView 在 vi​​ewDidLoad() 中,但这没有帮助。
  • 我做了非常相似的事情。我为 headerview 添加了一个 UILabel ,就像您在情节提要中所做的那样,并使用上面的函数来计算它的大小。只需尝试将所有这些都包装在一个视图中,并在 viewController 中对该视图进行强烈引用。这可能是您遇到的问题。另外,您介意检查一下您是否在viewDidLayoutSubviews 中收到tableView.tableHeaderView
  • 看来我做错了什么。现在它在return itemStore.allItems.count + 1 线上失败了Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value。我检查了调试器,变量var itemStore: ItemStore! 总是nil,如果添加tableView.tableHeaderView = headerView。我不知道它是如何相关的:(
  • 试着清理你的项目和你的派生数据。它可能会有所帮助。
【解决方案2】:

我找到了万恶之源。如果您像这样设置 ItemStore:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    guard let _ = (scene as? UIWindowScene) else { return }

    window = UIWindow(frame: UIScreen.main.bounds)
    window?.windowScene = (scene as! UIWindowScene)
    window?.rootViewController = ItemsViewController()
    window?.makeKeyAndVisible()

    // Create an ItemStore
    let itemStore = ItemStore()

    // Access the ItemsViewController and set its item store
    let itemsController = window!.rootViewController as! ItemsViewController
    itemsController.itemStore = itemStore
}

不要那样做。对我来说是这样的:

override func viewDidLoad() {
    super.viewDidLoad()

    itemStore = ItemStore()

    tableView.rowHeight = UITableView.automaticDimension
    tableView.estimatedRowHeight = 65
}

这本书似乎已经过时了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-06
    • 2013-05-04
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多