我只想在点击搜索栏时显示表格视图(比如instagram)。如果有人知道怎么做,任何信息都会有帮助。
这是我的相关代码:

class UserSearchController: UICollectionViewController, UICollectionViewDelegateFlowLayout,UISearchBarDelegate, UISearchDisplayDelegate {

let cellId = "cellId"

let searchBar: UISearchBar = {
    let sb = UISearchBar()
    sb.placeholder = "Search"
    return sb
}()

fileprivate func setupNavBarAndSearchBar() {

    let navBar = navigationController?.navigationBar
    searchBar.delegate = self
    navigationController?.navigationBar.addSubview(searchBar)
    //Constraints already figured^^
}


func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {searchBar.setShowsCancelButton(true, animated: true)}

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    searchBar.setShowsCancelButton(true, animated: true)

}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    searchBar.resignFirstResponder()
    searchBar.setShowsCancelButton(false, animated: true)
    searchBar.text = ""
}


override func viewDidLoad() {
    super.viewDidLoad()
    setupNavBarAndSearchBar()
    collectionView?.backgroundColor = .white
    collectionView?.register(UserProfileVideoCell.self, forCellWithReuseIdentifier: cellId)
  searchBar.addSubview(SearchUsersTv) **Compiles error "expected argument type UIView"**
}

以下是“SearchUsersTv”文件的内容:
class SearchUsersTv: UIView, UITableViewDelegate, UITableViewDataSource {
let cellId = "cellId"

var tableView = UITableView()

let screenHeight = UIScreen.main.bounds.height
let screenWidth = UIScreen.main.bounds.width

override init(frame: CGRect){
    super.init(frame: frame)

    setup()
    tableView.delegate = self
    tableView.dataSource = self
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
func setup() {

    self.backgroundColor = UIColor.black

    tableView = UITableView(frame: CGRect(x: 0, y: 0, width: screenWidth*0.5, height: screenHeight))
    tableView.layer.backgroundColor = UIColor.black.cgColor
    self.addSubview(tableView)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
    cell.backgroundColor = .magenta
    return cell
}

现在上面的这个文件没有运行,因为它没有运行(它编译
尝试将此添加为第一个文件中的子视图时出错)

最佳答案

在情节提要中,首先将TableView alpha值设置为“0”。
只需在你的

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {searchBar.setShowsCancelButton(true, animated: true)}

这样地
tableView.alpha = 1.0

希望这能帮到我。它也解决了我的问题。

关于ios - 仅在单击搜索栏时如何显示表格 View ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45908469/

10-10 21:07