在 Swift 中显示 UITableViewController 中的 UICollectionView
Show UICollectionView within UITableViewController in Swift
这几天我一直在努力让它工作。我想在 UITableViewController class 中显示一个 UICollectionView。我有一个搜索栏来过滤项目,但是当我关闭搜索栏时,我希望显示集合视图。我已经包含了我的全部代码,也许你们中的某个人可以帮助我让它工作。我尝试在不同的地方调用 func configureCollectionView,但其中 none 似乎有效。我注册的所有 Cell 都运行良好(我知道,因为它们在其他控制器中运行..)
谢谢!!
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()
}
然后是提取函数,它们按预期工作。
最好创建 table 包含集合视图的单元格。
您正在将集合视图添加到 table 视图。如果隐藏 table 视图,所有子视图也将被隐藏。改为将 collectionview 添加到控制器的视图中。
改变
tableView.addSubview(collectionView)
至
view.addSubview(collectionView)
在普通视图控制器中同时处理(table 和 collectionview)会更容易。
这几天我一直在努力让它工作。我想在 UITableViewController class 中显示一个 UICollectionView。我有一个搜索栏来过滤项目,但是当我关闭搜索栏时,我希望显示集合视图。我已经包含了我的全部代码,也许你们中的某个人可以帮助我让它工作。我尝试在不同的地方调用 func configureCollectionView,但其中 none 似乎有效。我注册的所有 Cell 都运行良好(我知道,因为它们在其他控制器中运行..)
谢谢!!
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()
}
然后是提取函数,它们按预期工作。
最好创建 table 包含集合视图的单元格。
您正在将集合视图添加到 table 视图。如果隐藏 table 视图,所有子视图也将被隐藏。改为将 collectionview 添加到控制器的视图中。
改变
tableView.addSubview(collectionView)
至
view.addSubview(collectionView)
在普通视图控制器中同时处理(table 和 collectionview)会更容易。