ios - 如何以编程方式在 TableViewCell 中添加 SearchBar?/迅速
问题描述
我试图在我的 TableView 单元格的第一行中放置一个 searchBar,但它只会抛出一个错误,但我不明白为什么,有人可以告诉我该怎么做吗?提前致谢!
//自定义单元格
class SearchCell: UITableViewCell, UISearchBarDelegate {
let searchbar = UISearchBar()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview(searchbar)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setConstraints() {
searchbar.placeholder = "Country, City"
searchbar.delegate = self
searchbar.searchBarStyle = .minimal
//Constraints
searchbar.translatesAutoresizingMaskIntoConstraints = false
searchbar.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
searchbar.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10).isActive = true
searchbar.widthAnchor.constraint(equalToConstant: 280).isActive = true
searchbar.heightAnchor.constraint(equalToConstant: 40).isActive = true
}
override func awakeFromNib() {
super.awakeFromNib()
setConstraints()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
这是包含 TableView 的 ViewController:
class SearchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
!Line 20! let cell = tableView.dequeueReusableCell(withIdentifier: "SearchCell") as! SearchCell
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
return cell
}
}
let tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
//
view.backgroundColor = .white
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.title = "Search"
//Functions
configureTableView()
}
func configureTableView() {
view.addSubview(tableView)
tableView.rowHeight = 80
//Constraints
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
tableView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
tableView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
tableView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
}
}
我看了一些教程等,但它们对我没有帮助,所以谢谢你们!
解决方案
您需要先将您的单元格及其reuseIdentifier 注册到您的tableview 中。我在您发布的代码中看不到这一点。
super.viewDidLoad()
添加类似的东西后
tableView.register(SearchCell.self, forCellReuseIdentifier: "SearchCell")
推荐阅读
- java - 在绘制路径中使用箭头键移动图形对象
- docker - 启动时无法跳转到 nginx docker 容器
- python - 不明确地访问 SQLAlchemy 类字段并修复“对象不可下标”错误
- c# - 如何在 SQL Server 中存储以任何编程语言编写的代码/程序
- css - 关键/延迟 CSS 实现
- java - 连接到 sqlite 后我无法执行操作。它立即断开连接
- python - Concat数据帧行明智并合并行(如果存在)
- html - 我的徽标不会出现在标题上,正确的文件路径是什么?
- python - 如何使用增量跳过打印一张两张表
- mysql - MySQL - 如果其中一个包含值,则更新一系列分组行(即使只有一个有库存,也将所有产品设置为有效)