使用 Swift 标头中的分段控件更改 UICollectionView 的内容 [英] Changing content of UICollectionView with segmented Control in the header in Swift

查看:28
本文介绍了使用 Swift 标头中的分段控件更改 UICollectionView 的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I tried to solve this problem since hours and didn't find a proper solution. I want to have a custom UICollectionView with a segmented control in the header. Changing the segmented control index should render the cells differently. So far I can display the segmented control in the header of my collectionView but changing the content of the collectionView inside of my UserSearchHeader doesn't work.

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

I created a custom UICollectionView called UserSearchController which is a Subclass of UICollectionView and conforms the UICollectionViewDelegateFlowLayout protocol.

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

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

My custom collectionView UserSearchController has a custom header (UserSearchHeader) and custom cells (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 at 方法来检查分段控件的状态.但是,当在 UserSearchHeader 类中定义分段控件时,我该如何做到这一点.

If the segmentedIndex is 0 I want to render the cells with the UserSearchCell class if the segmentedIndex is 1 I want to render it with a different cellClass (Clicking the segmented control). How can I achieve this behavior with these different classes. shall I use the cellForItem at method inside of the UserSearchController to check the status of the segmented control. But how can I do this when the segmented control is defined in the UserSearchHeader class .

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

推荐答案

尝试使用 scopeButtonTitles 属性?

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

看看 这个问题了解更多详情.基本上你使用

Have a look at this question for more details. Basically you use the

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 添加标头.

Please find below the missing pieces described in the link provided initially. There is no need to add the header from a NIB.

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

First, you add properties for your search & results controllers:

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() 
} 

然后你需要在你的类中添加这个函数:

Then you need to add this function to your class:

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:

Finally, you add the function I was describing initially: searchBar:selectedScopeButtonIndexDidChange:

这篇关于使用 Swift 标头中的分段控件更改 UICollectionView 的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆