Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift标题中使用分段控件更改UICollectionView的内容_Ios_Swift_Uicollectionview_Uicontrol - Fatal编程技术网

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

Ios 在Swift标题中使用分段控件更改UICollectionView的内容,ios,swift,uicollectionview,uicontrol,Ios,Swift,Uicollectionview,Uicontrol,我几个小时以来一直试图解决这个问题,但没有找到合适的解决办法。我想要一个自定义UICollectionView,在标题中有一个分段控件。更改分段控制索引应以不同方式呈现单元格。到目前为止,我可以在collectionView的标题中显示分段控件,但在UserSearchHeader中更改collectionView的内容是行不通的 我创建了一个名为UserSearchController的自定义UICollectionView,它是UICollectionView的子类,符合UICollecti

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

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

        class UserSearchController: UICollectionViewController, 
              UICollectionViewDelegateFlowLayout, UISearchBarDelegate { ....
My custom collectionViewUserSearchController具有自定义标题(UserSearchHeader)和自定义单元格(UserSearchCell

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 ....}

是否尝试使用
scopebuttonitles
属性

mySearchController.searchBar.scopeButtonTitles = ["Username", "Hashtag"]
请查看以了解更多详细信息。基本上你使用

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:

是的,您可以轻松执行该操作,更改选定索引后,您需要重新加载集合视图,在您的cellforrow中,在索引路径处检查所选索引是否为零,返回cell1,如果所选索引为1,返回Cell2。我尝试了此方法,但不起作用,因为segmentedControl是在UserSearchHeader中定义的。内容不会更改,因为分段控件的targetAction未执行OK,然后制定协议,在选定索引更改时通知您,并同时传递选定索引。谢谢,但我不知道如何执行,你能给我一个基于我的类的简短示例吗?它会对我有很大帮助。好的,给我发代码,如果你能,我会这样做。我的搜索栏在我的导航栏中,当添加属性ScopeButton时,它没有显示任何内容,只是一个灰色背景。如果我把搜索栏放在我的collectionView的标题中,它又在另一个类中,我没有访问权限。如何在导航栏中显示带有ScopeButtonTitles的搜索栏。如何自动增加导航栏的高度?请检查UISearchController的属性。在我的示例中,它被称为mySearchController。如您所见,searchBar属性是navigationBar的一个属性。查看UISearchController的属性名称以及添加搜索栏的方式。在提供的链接中,您有一个示例。请看一看,因为添加搜索栏也是一行。不,它不起作用。请参见:。如何使用分段控制解决此问题。
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 
}