【问题标题】:Show UICollectionView within UITableViewController in Swift在 Swift 中的 UITableViewController 中显示 UICollectionView
【发布时间】:2021-10-06 09:33:00
【问题描述】:

几天来,我一直在努力让它发挥作用。我想在 UITableViewController 类中显示 UICollectionView。我有一个搜索栏来过滤项目,但是当我关闭搜索栏时,我希望出现 collectionview。我已经包含了我的整个代码,也许你们中的某个人可以帮助我让它工作.. 我尝试在不同的地方调用 func configureCollectionView,但它们似乎都不起作用。我正在注册的所有单元都可以正常工作(我知道,因为它们可以在其他控制器中工作..)

谢谢!!

 private let reuseIdentifier = "SearchThisCell"

class SearchController: UITableViewController, UISearchBarDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {


var item1 = [Item1]()
var searchbar = UISearchBar()
var filteredItem = [Item1]()
var inSearchMode = false
var collectionView: UICollectionView!
var collectionViewEnabled = true
var item2 = [Item2]()

override func viewDidLoad() {
    super.viewDidLoad()
    
    // register cell classes
    tableView.register(SearchThisCell.self, forCellReuseIdentifier: reuseIdentifier)

    tableView.separatorInset = UIEdgeInsets(top: 0, left: 75, bottom: 0, right: 75)
    tableView.separatorStyle = .none
    
    configureSearchBar()
    
    configureCollectionView()
    
    fetchItem1()
    
    fetchItem2()
            
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 70
}

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if inSearchMode {
        return filteredItem.count
    } else {
        return item1.count
    }
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    var item: Item1!
    
    if inSearchMode {
        item = filteredItem[indexPath.row]
    } else {
        item = item1[indexPath.row]
    }
            
    let itemProfileVC = ItemProfileController(collectionViewLayout: UICollectionViewFlowLayout())
    
    itemProfileVC.item = item
    
    navigationController?.pushViewController(itemProfileVC, animated: true)
    
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! SearchThisCell
    
    var item: Item1!
    
    if inSearchMode {
        item = filteredItem[indexPath.row]
    } else {
        item = item1[indexPath.row]
    }
    
    cell.item = item1
    
    return cell
}

func configureCollectionView() {
    
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical

    let frame = CGRect(x: 50, y: 50, width: view.frame.width, height: view.frame.height - (tabBarController?.tabBar.frame.height)! - (navigationController?.navigationBar.frame.height)!)

    collectionView = UICollectionView(frame: frame, collectionViewLayout: layout)
    collectionView.delegate = self
    collectionView.dataSource = self
    collectionView.alwaysBounceVertical = true
    collectionView.backgroundColor = .white
    collectionView.register(SearchThatCell.self, forCellWithReuseIdentifier: "SearchThatCell")

    tableView.addSubview(collectionView)
    tableView.separatorColor = .clear
    
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 1
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 1
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let width = (view.frame.width - 2) / 3
    return CGSize(width: width, height: width)
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SearchThatCell", for: indexPath) as! SearchThatCell
    
    cell.that = item2[indexPath.item]
    
    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
    let overviewVC = OverviewController(collectionViewLayout: UICollectionViewFlowLayout())
    
    overviewVC.viewSingleItem = true
    
    overviewVC.that = item2[indexPath.item]
    
    navigationController?.pushViewController(overviewVC, animated: true)
    
}

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    searchbar.showsCancelButton = true
    
    collectionView.isHidden = false
    collectionViewEnabled = true
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    
    let searchText = searchText.lowercased()
    
    if searchText.isEmpty || searchText == " " {
        inSearchMode = false
        tableView.reloadData()
    } else {
        inSearchMode = true
        filteredItem = item.filter({ (item) in
            return item.itemname.contains(searchText)
        })
        tableView.isHidden = false
        tableView.reloadData()
    }
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    searchbar.endEditing(true)
    
    searchbar.showsCancelButton = false
    
    inSearchMode = false
    
    searchbar.text = nil
    
    collectionViewEnabled = true
            
    configureCollectionView()

    tableView.isHidden = true
    
    tableView.reloadData()
}

然后是 fetch 函数,它们按预期工作。

【问题讨论】:

  • 添加一个collectionview、一个tableView和一个searchbar作为UIViewController的子视图。正确设置约束。根据搜索关键字隐藏/取消隐藏表格视图。这将是一种解决方案。
  • 我正在尝试您的解决方案,但出现此错误:线程 1:致命错误:在隐式展开可选值时意外发现 nil
  • 我将如何正确地解决您的问题? @AkilanCB

标签: ios swift uitableview uicollectionview swift5


【解决方案1】:

您正在将集合视图添加到表格视图中。如果隐藏表格视图,所有子视图也将被隐藏。而是将 collectionview 添加到控制器的视图中。

改变

tableView.addSubview(collectionView)

view.addSubview(collectionView)

在普通视图控制器中处理(表和集合视图)会更容易。

【讨论】:

  • 你,先生@baronfac,太棒了!它终于工作了!非常感谢!!!
  • 很高兴我能帮上忙 :)
【解决方案2】:

最好是创建带有集合视图的表格单元格。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多