我正在通过“ 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
}
}
可能与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
}