访问UICollectionView标题单元格中的搜索栏 - Swift

时间:2014-08-27 12:59:50

标签: 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
}

如何在 UICollectionViewClass 中访问UISearchBar属性,包括 delegates text

1 个答案:

答案 0 :(得分:1)

我回答假设您SearchBarCollectionReusableView内有UICollectionViewClass的实例,假设它是UIView扩展课程。

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

searchBarCRV.searchBar.delegate = self;

和文字......

var searchBarText = searchBarCRV.searchBar.text

...在UICollectionViewClass内。

相关问题