在Swift的头文件中使用分段控件更改UICollectionView的内容

时间:2017-05-24 09:28:45

标签: ios swift uicollectionview uicontrol

我试图解决这个问题,因为几个小时,并没有找到一个合适的解决方案。我想在标题中有一个带有分段控件的自定义UICollectionView。更改分段控制索引应该以不同方式呈现单元格。到目前为止,我可以在我的collectionView的标题中显示分段控件,但是在我的UserSearchHeader中更改collectionView的内容不起作用。

我创建了一个名为 UserSearchController 的自定义UICollectionView,它是UICollectionView的子类,并且符合UICollectionViewDelegateFlowLayout协议。

        class UserSearchController: UICollectionViewController, 
              UICollectionViewDelegateFlowLayout, UISearchBarDelegate { ....

我的自定义collectionView UserSearchController 有自定义标题( UserSearchHeader )和自定义单元格( UserSearchCell )。

  collectionView?.register(UserSearchCell.self, forCellWithReuseIdentifier: cellId)
  collectionView?.register(UserSearchHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerId")

UserSearchHeader 包含分段控件。

   class UserSearchHeader: UICollectionViewCell {

var segmentedControl: UISegmentedControl = {
    let sc = UISegmentedControl(items: ["Username", "Hashtag"])
    sc.translatesAutoresizingMaskIntoConstraints = false
    sc.tintColor = UIColor.black
    sc.selectedSegmentIndex = 0 // automatically highlight
    //  sc.addTarget(self, action: #selector(segmentedChange), for: .valueChanged)
    return sc
}() .....

如果segmentedIndex为0,我想使用UserSearchCell类渲染单元格,如果segmentedIndex为1,我想用不同的cellClass渲染它(单击分段控件)。如何使用这些不同的类实现此行为。我应该在UserSearchController内部的方法中使用cellForItem来检查分段控件的状态。但是,如果在UserSearchHeader类中定义了分段控件,我该怎么做呢?

       override func collectionView(_ collectionView: 
    UICollectionView, cellForItemAt indexPath: IndexPath) -> 
  UICollect. ionViewCell { if segmentedControl.segmentedIndex = 0 ....}

1 个答案:

答案 0 :(得分:0)

尝试使用scopeButtonTitles属性?

mySearchController.searchBar.scopeButtonTitles = ["Username", "Hashtag"]

有关详细信息,请查看this question。基本上你使用

func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) { 
    // respond to your scopeBar selection here
    switch selectedScope {
    case 0:
        print("Username tab selected")
        // do your stuff for Username here
    case 1:
        print("Hashtag tab selected")
        // do your stuff for Hashtag here
    default:
        print("Someone added a tab!")
        // you shouldn't have more than 2 tabs
}

编辑: 请在下面找到最初提供的链接中描述的缺失部分。无需从NIB添加标头。

首先,为搜索添加属性&结果控制器:

typealias ResultVC = UserResultController //The class to show your results
var searchController: UISearchController! 
var resultController: ResultVC? 

然后,在viewDidLoad中添加以下内容:

// Search Results 
resultController = ResultVC() 
setupSearchControllerWith(resultController!) 
if #available(iOS 9.0, *) { 
    searchController?.loadViewIfNeeded() 
} 

然后你需要将这个功能添加到你的班级:

func setupSearchControllerWith(_ results: ResultVC) { 
    // Register Cells 
    results.tableView.register(TableCell.self, forCellReuseIdentifier: "\(TableCell.self)") 
    results.tableView.register(UINib(nibName: "\(TableCell.self)", bundle: Bundle.main), forCellReuseIdentifier: "\(TableCell.self)") 

  // We want to be the delegate for our filtered table so didSelectRowAtIndexPath(_:) is called for both tables. 
    results.tableView.delegate = self 

    searchController = UISearchController(searchResultsController: results) 

    // Set Scope Bar Buttons 
    searchController.searchBar.scopeButtonTitles = ["Comics only", "Digital too"] 

    // Set Search Bar 
    searchController.searchResultsUpdater = self 
    searchController.searchBar.sizeToFit() 
    tableView.tableHeaderView = searchController.searchBar 

    // Set delegates 
    searchController.delegate = self 
    searchController.searchBar.delegate = self    // so we can monitor text & scope changes 

    // Configure Interface 
    searchController.dimsBackgroundDuringPresentation = false 
    searchController.hidesNavigationBarDuringPresentation = true 
    if #available(iOS 9.1, *) { 
        searchController.obscuresBackgroundDuringPresentation = false 
    }  

    // Search is now just presenting a view controller. As such, normal view controller 
    // presentation semantics apply. Namely that presentation will walk up the view controller 
    // hierarchy until it finds the root view controller or one that defines a presentation context. 
    definesPresentationContext = true 
} 

最后,添加我最初描述的函数:searchBar:selectedScopeButtonIndexDidChange: