【发布时间】: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