2017-08-27 138 views
0

我只想在点击搜索栏时才显示表格视图(如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 
} 

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

+5

在'viewDidLoad'中将'tableView'的'isHidden'属性设置为'true',并在'searchBarTextDidBeginEditing'中将其设置为'false'。您应该在'searchBarCancelButtonClicked'中将其设置回'true'。 –

+0

@ MoeAbdul-Hameed但我如何在集合视图的顶部添加一个表视图? –

+1

您正在以编程方式添加您的表格视图,因此您需要以编程方式添加其约束。 –

回答

0

在StoryBoard中首先将您的TableView alpha值设置为“0”。

简单地让它 “1.0” 在你

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

这样

tableView.alpha = 1.0 

希望这helps.It解决我的问题也。

相关问题