搜索表格视图时如何隐藏集合视图?

Posted

技术标签:

【中文标题】搜索表格视图时如何隐藏集合视图?【英文标题】:How to hide collection view when table view is searching? 【发布时间】:2019-03-13 17:06:51 【问题描述】:

我的第一个表格视图单元格中有集合视图。表格视图顶部的搜索栏(表格视图外)。集合视图显示为表格视图单元格项目计数viewDidLoad()。例如。如果表格视图单元格项目计数为 4,则集合视图项目计数为 4。

class MainViewController: UIViewController, UITableViewDataSource,  UITableViewDelegate,
UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout 

@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var mainTableView: UITableView!

var imageNames = [ImageNames]()
var priceFood: [Double]!
var searchFoods = [String]()
var filtered = [String]()
var searching = false


override func viewWillAppear(_ animated: Bool) 
    super.viewWillAppear(animated)
    self.tabBarController?.tabBar.isHidden = false


override func viewDidLoad() 
    super.viewDidLoad()

    self.navigationController?.navigationBar.isHidden = true
    let foodCell = Food(name: ["Hamburger big mac",
                               "Patates",
                               "Whopper",
                               "Steakhouse"], price: [15.0, 20.0, 25.0, 30.0])

    searchBar.delegate = self

    searchFoods = foodCell.name
    priceFood = foodCell.price

    imageNames = [
        ImageNames(name: "images"),
        ImageNames(name: "unnamed"),
        ImageNames(name: "unnamed")
        //            ImageNames(name: "images"),
        //            ImageNames(name: "images")
    ]



func numberOfSections(in tableView: UITableView) -> Int 
    return 2


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    if searching 
     return filtered.count
     else 
     return searchFoods.count
    


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    if indexPath.section == 0 
        let cell = tableView.dequeueReusableCell(withIdentifier: "MainFoodTableViewCell", for: indexPath) as! MainFoodTableViewCell

        //            cell.mainFoodCollectionView.delegate = self
        //            cell.mainFoodCollectionView.dataSource = self
        //            cell.mainFoodCollectionView.reloadData()
        cell.mainFoodCollectionView.tag = indexPath.row
        return cell

     else 

        let cell = tableView.dequeueReusableCell(withIdentifier: "CellForFood", for: indexPath) as! MainFoodTitleTableViewCell

        if searching 
            cell.titleLabel?.text = filtered[indexPath.row]
            cell.priceLabel?.text = priceFood[indexPath.row].description
         else 
            cell.titleLabel?.text = searchFoods[indexPath.row]
            cell.priceLabel?.text = priceFood[indexPath.row].description
        
        return cell
    



func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
    return indexPath.section == 0 ? 130 : 65


func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat 
    return indexPath.section == 0 ? 100 : 65


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
    return  imageNames.count


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize 
    let width = UIScreen.main.bounds.width
    return CGSize(width: width, height: 130)



func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MainFoodCollectionViewCell", for: indexPath) as! MainFoodCollectionViewCell
    let img = imageNames[indexPath.row]
    cell.mainFoodImage.image = UIImage(named: img.name)
    return cell



extension MainViewController : UISearchBarDelegate 

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) 

    self.searchBar.showsCancelButton = true


func searchBarCancelButtonClicked(_ searchBar: UISearchBar) 
    searching = false
    searchBar.showsCancelButton = false
    searchBar.text = ""
    searchBar.resignFirstResponder()
    mainTableView.reloadData()
   

我认为我对这个功能没有意义

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) 

    filtered = searchText.isEmpty ? searchFoods : filtered.filter  (item: String) -> Bool in
        return item.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil
    
    //        filtered = self.searchFoods.filter ($0.prefix(searchText.count) == searchText)
    searching = true
    mainTableView.reloadData()



【问题讨论】:

等等,你只是想隐藏收藏视图。如果是这样,只需使用 collectionView.isHidden = true 部分是的,但我怎样才能隐藏在searchBar(:, textDidChange) 方法中? 好吧,我还没有真正使用过搜索栏,但我猜你可以在搜索时把它放在那里 = true 如果我将集合单元格隐藏起来,它们的高度或大小与视图背景一起放置在那里:) 那我不确定。对不起:( 【参考方案1】:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
if indexPath.section == 0 
    let cell = tableView.dequeueReusableCell(withIdentifier: "MainFoodTableViewCell", for: indexPath) as! MainFoodTableViewCell
    cell.mainFoodCollectionView.tag = indexPath.row
    return cell

 else 

    let cell = tableView.dequeueReusableCell(withIdentifier: "CellForFood", for: indexPath) as! MainFoodTitleTableViewCell

    if searching 
        cell.titleLabel?.text = filtered[indexPath.row]
        cell.priceLabel?.text = priceFood[indexPath.row].description
     else 
        cell.titleLabel?.text = searchFoods[indexPath.row]
        cell.priceLabel?.text = priceFood[indexPath.row].description
    
    return cell



解决了我的问题

【讨论】:

以上是关于搜索表格视图时如何隐藏集合视图?的主要内容,如果未能解决你的问题,请参考以下文章

如何按选定范围从集合视图更改为表格视图?

更改集合视图的 isHidden 属性不适用于搜索栏取消按钮

如何在表格视图单元格内创建集合视图

如何在可扩展的表格视图单元中制作动态集合视图

如何使用按钮从集合视图(网格视图)切换到表格视图(列表视图)?

如何使用 Swift 在集合视图单元格中添加表视图?