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

时间:2019-03-13 17:06:52

标签: ios swift uitableview uicollectionview

enter image description here

我在第一个表格视图单元格中有集合视图。表格视图顶部的搜索栏(表格视图之外)。显示为表格视图单元格项目的集合视图计数为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()

}
}

1 个答案:

答案 0 :(得分:0)

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
}

}

解决了我的问题