如何解决我正在尝试在 TableViewController 中加载 collectionView 并在 tableView 的子视图中添加 collectionView
这是配置tableView控制器的完整代码。
class SearchViewControllerTableViewController: UITableViewController,UISearchBarDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
var users = [User]()
var filteredUsers = [User]()
var searchBar = UISearchBar()
var inSearchMode = false
var collectionView: UICollectionView!
var collectionViewEnabled = true
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(SearchUserCellTableViewCell.self,forCellReuseIdentifier: reusableIdentifier)
tableView.separatorStyle = .none
tableView.alwaysBounceVertical = true
tableView.keyboardDismissMode = .onDrag
configureSearchBar()
configureCollectionView()
fetchUsers()
print("Collection View \(collectionView.isHidden)")
}
这是配置collectionView的代码。
func configureCollectionView() {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
let frame = CGRect(x: 0,y: 0,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
tableView.addSubview(collectionView)
collectionView.register(SearchPostCell.self,forCellWithReuseIdentifier: reusableIdentifierCollectionView)
}
这是 tableView 的代码。这会在应用中加载一些用户。
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
if inSearchMode {
return filteredUsers.count
} else {
return users.count
}
}
override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reusableIdentifier,for: indexPath) as! SearchUserCellTableViewCell
var user: User!
if inSearchMode {
user = filteredUsers[indexPath.row]
} else {
user = users[indexPath.row]
}
cell.user = user
cell.selectionStyle = .none
return cell
}
override func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
return 69
}
override func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
var user: User!
if inSearchMode {
user = filteredUsers[indexPath.row]
} else {
user = users[indexPath.row]
}
// Instance for user profile VC
let userProfileView = UserProfileCollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())
// Assign the selected user.
userProfileView.user = user
// Navigate to the selected user's profile
navigationController?.pushViewController(userProfileView,animated: true)
userProfileView.setNavigationBar()
}
这是 collectionView 数据源和委托的代码。
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 15
}
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reusableIdentifierCollectionView,for: indexPath) as! SearchPostCell
return cell
}
在 tableView 之上,我想添加一个默认情况下应该隐藏的 CollectionView。
但是 collectionView 没有出现。但是在加载视图“collectionView.isHidden”时返回false
解决方法
您不应尝试将子视图直接添加到表视图或集合视图中。
这一行:tableView.addSubview(collectionView)
行不通。
此外,UITableViewController 和 UICollectionViewController 是非常专业的视图控制器,它们只会管理表视图/集合视图。您不能向这些视图控制器类添加其他子视图。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。