ios - 在 Swift 中使用标题中的分段控件更改 UICollectionView 的内容

标签 ios swift uicollectionview uicontrol

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

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

        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 at 方法来检查分段控件的状态吗?但是,当 UserSearchHeader 类中定义了分段控件时,我该如何执行此操作。

       override func collectionView(_ collectionView: 
    UICollectionView, cellForItemAt indexPath: IndexPath) -> 
  UICollect. ionViewCell { if segmentedControl.segmentedIndex = 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 添加 header 。

首先,为搜索和结果 Controller 添加属性:

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:

关于ios - 在 Swift 中使用标题中的分段控件更改 UICollectionView 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44154384/

相关文章:

ios - Swift 2.0 迁移代码错误

swift - ORKTaskViewController - isNavigationBarHidden

ios - UICollectionView 不调用委托(delegate)方法

ios - 画一条直线 iOS

ios - UIButtons 数组,出现错误 : "Unexpectedly found nil while unwrapping optional value"

ios - 尝试让按钮与 TextField iOS 配合使用

ios - 水平滚动的 UICollectionViewCell 内部的 UITableView 垂直滚动问题

ios - 在 UITableView 中嵌入了不同数量的项目的 UICollectionViews 索引超出范围

ios - 使用 iOS 7.1 的 UIImageView 子类中的 setter/getter 的不同行为

ios - 核心数据在两个属性上查找单个匹配项