Ios 如何以编程方式在TableViewCell中添加搜索栏/敏捷的
我试图在TableView单元格的第一行中放置一个搜索栏,但它只会抛出一个错误,但我不明白为什么,有人能告诉我该怎么做吗?提前谢谢 //自定义单元格Ios 如何以编程方式在TableViewCell中添加搜索栏/敏捷的,ios,swift,uitableview,Ios,Swift,Uitableview,我试图在TableView单元格的第一行中放置一个搜索栏,但它只会抛出一个错误,但我不明白为什么,有人能告诉我该怎么做吗?提前谢谢 //自定义单元格 class SearchCell: UITableViewCell, UISearchBarDelegate { let searchbar = UISearchBar() override init(style: UITableViewCell.CellStyle, reuseIdentifier: Strin
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
}
}
我看了一些教程等,但他们没有帮助我,所以谢谢你们 您需要首先在tableview中向其reuseIdentifier注册单元格。我在你发布的代码中没有看到这一点 在
super.viewDidLoad()之后添加如下内容
tableView.register(SearchCell.self,forCellReuseIdentifier:“SearchCell”)
但有什么错误?你还没告诉我们,所以我们不知道你需要什么帮助。当然,我很抱歉。。错误是“线程1:致命错误:在解包可选值时意外发现nil”,在第20行中,我在代码中标记了第20行可能您的单元格不是SearchCell@ChrisCode-您是否将故事板中的原型单元设置为SearchCell
类,并为其提供了一个重用标识符“SearchCell”?现在它在AppDelegate中抛出了以下错误:线程1:“无法使用标识符单元格对单元格进行出列-必须为标识符注册nib或类,或者在情节提要中连接原型单元格”是有意义的。您的代码显示let cell=tableView.dequeueReusableCell(带有标识符:“cell”,用于:indexPath);如果行不为零,则返回单元格
。因此,您要么返回一个默认单元格,要么向reuseIdentifier“cell”注册一个单元格。基本上,你又在问同样的问题。哦,这是合乎逻辑的