2017-08-27 133 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解決我的問題也。

相關問題