首页 > 解决方案 > 如何以编程方式在 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
    }
        
        
        
    
}

我看了一些教程等,但它们对我没有帮助,所以谢谢你们!

标签: iosswiftuitableview

解决方案


您需要先将您的单元格及其reuseIdentifier 注册到您的tableview 中。我在您发布的代码中看不到这一点。

super.viewDidLoad()添加类似的东西后

tableView.register(SearchCell.self, forCellReuseIdentifier: "SearchCell")


推荐阅读