问题描述
我目前有一个视图,可以在其中显示具有某些类别的 collectionView.我按照这篇文章让我的collectionView正确加载显示数据.我让它工作得很好.
I currently have a view where I display a collectionView with some categories. I followed this post to get my collectionView to properly load display data. I got that working just fine.
我的问题是,现在我需要能够从用户选择在我的搜索栏中使用的任何单元格中获取文本.
My issue is that now I need to be able to get the text from whichever cell the user selects to use within my search bar.
我当前的代码:
class SearchViewController: ButtonBarPagerTabStripViewController, UISearchBarDelegate{
let headerId = "sectionHeader"
let categoriesId = "categoriesId"
lazy var searchBar: UISearchBar = {
let sb = UISearchBar()
sb.placeholder = "Search businesses or coupons"
sb.barTintColor = .gray
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = UIColor.mainWhite()
sb.delegate = self
return sb
}()
lazy var searchCategriesView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.headerReferenceSize = CGSize(width: view.frame.width, height: 75)
layout.itemSize = CGSize(width: view.frame.width, height: 50)
layout.minimumInteritemSpacing = 1
layout.minimumLineSpacing = 1
layout.scrollDirection = .vertical
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.mainWhite()
cv.translatesAutoresizingMaskIntoConstraints = false
return cv
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
categoriesDataSource = SearchCategories()
searchCategriesView.dataSource = categoriesDataSource
searchCategriesView.register(SearchCategoriesCell.self, forCellWithReuseIdentifier: categoriesId)
searchCategriesView.register(SectionHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerId)
}
//EXCLUDED VIEW SETUP CODE FOR BREVITY
}
根据上面提到的链接,我在另一个文件中设置了我的数据源,如下所示:
And as per the link mentioned above, I set up my data source in another file like this:
class SearchCategories: NSObject {
let categories = ["Apparel & Clothing", "Arts & Crafts", "Automotive",
"Baby", "Bars & Lounges", "Beauty", "Books",
"Entertainment",
"Family & Children", "Furniture",
"Grocery",
"Health", "Home & Garden", "Home improvement",
"Pets", "Pizza",
"Restaurants",
"Sporting Goods"]
let headerId = "sectionHeader"
let categoriesId = "categoriesId"
override init() {
}
}
extension SearchCategories: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return categories.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: categoriesId, for: indexPath) as! SearchCategoriesCell
cell.label.text = categories[indexPath.item]
return cell
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let sectionHeaderView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerId, for: indexPath) as! SectionHeaderView
if indexPath.section == 0 {
sectionHeaderView.categoryTitleLabel.text = "Search Categories"
}
return sectionHeaderView
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: categoriesId, for: indexPath) as! SearchCategoriesCell
//NEED TO PASS THIS DATA BACK TO SEARCHVIEWCONTROLLER
}
}
我现在如何在用户点击 collectionView 单元格时在 SearchViewController 中取回单元格文本?
推荐答案
假设你将这个函数中的文本保存在 SearchViewController
Suppose you will hold the text in this function inside SearchViewController
func send(_ res:String){
print(res)
}
1- 在里面添加一个 var
1- Add a var inside
class SearchCategories: NSObject {
weak var delegate:SearchViewController?
2- 设置委托
categoriesDataSource = SearchCategories()
categoriesDataSource.delegate = self
3- 发送点击的文本
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let sendedText = categories[indexPath.item]
delegate?.send(sendedText)
}
您需要添加
searchCategriesView.delegate = categoriesDataSource
在 viewDidLoad
中你也可以做
searchCategriesView.delegate = self
并在 SearchViewController
这篇关于如何从 UICollectionView 的外部数据源获取单元格标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!