如何从UICollectionView的外部数据源获取单元格标题

时间:2018-11-11 23:10:57

标签: ios swift uicollectionview

我目前有一个视图,其中显示带有某些类别的collectionView。我遵循this post来获取我的collectionView以正确加载显示数据。我做的很好。

我的问题是,现在我需要能够从用户选择在搜索栏中使用的任何单元格获取文本。

我当前的代码:

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
}

按照上面提到的链接,我将数据源设置在另一个文件中,如下所示:

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中重新获得单元格文本?

1 个答案:

答案 0 :(得分:0)

假设您将在SearchViewController

中的此函数中保存文本
func send(_ res:String){
  print(res)
}

1-在其中添加var

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

并在didSelectItemAt内实现SearchViewController