ios - 在 CollectionView 标题单元格中访问 SearchBar - Swift

标签 ios swift uisearchbar uicollectionviewcell collectionview

目前我在 CollectionView 标题单元格上实现了一个简单的 UICollectionReusableView 类:

class SearchBarCollectionReusableView: UICollectionReusableView {

    @IBOutlet weak var searchBar:UISearchBar!
    @IBOutlet weak var filterSegCtrl:UISegmentedControl!

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

然后我将它添加到我的 UICollectionViewClass 以显示下面的标题:

override func collectionView(collectionView: UICollectionView!, viewForSupplementaryElementOfKind kind: String!, atIndexPath indexPath: NSIndexPath!) -> UICollectionReusableView! {

    var reusableview:UICollectionReusableView!

    if kind == UICollectionElementKindSectionHeader {
        let headerView:SearchBarCollectionReusableView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "SearchBar", forIndexPath: indexPath) as SearchBarCollectionReusableView
        reusableview = headerView
    }
    return reusableview
}

如何访问 UISearchBar 属性,包括 UICollectionViewClass 中的委托(delegate)文本

最佳答案

我的回答假设您在 UICollectionViewClass 中有一个 SearchBarCollectionReusableView 实例,假设它是您的 UIView扩展类。

假设您的 SearchBarCollectionReusableView 变量是 searchBarCRV。由于所有实例级变量在 Swift 中都是公开的,因此您可以直接访问其实例变量。您可以通过类似的方式获得代表,

searchBarCRV.searchBar.delegate = self;

和文本...

var searchBarText = searchBarCRV.searchBar.text

... 在您的 UICollectionViewClass 中。

关于ios - 在 CollectionView 标题单元格中访问 SearchBar - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25527868/

相关文章:

objective-c - Swift 不从框架中获取 NSMutableArray 类型注释

ios - 重新创建 Quora 应用程序 iOS 7 UISearchBar

ios - 如何将纹理传递给顶点着色器? (iOS 和 Metal )(IOAF 代码 5)

iphone - 在 App Purchase 用户取消 tx 而应用程序在后台 : tx state stays on purchasing

java - 从 Cloud Firestore 获取 map 数据

swift - 将缓存的远程音频文件加载到 iOS 上的 AKPlayer,而不是使用本地文件

ios - 推送时从UISearchController删除搜索栏

ios - 使用 UISearchController 搜索 TableView

iOS 通用链接 : Tapping universal link inside the Application redirects me to safari instead of continuing in the application

ios - 以编程方式调整 UICollectionViewCell View 的位置在哪里?