如何以编程方式在 TableViewCell 中添加 SearchBar? /迅速

问题描述

我尝试将 searchBar 放入 TableView Cells 的第一行,但它只会引发错误,但我不明白为什么,有人能告诉我该怎么做吗?提前致谢!

//自定义单元格

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")