Fudgey
Fudgey

Reputation: 3833

Accessing SearchBar in CollectionView Header Cell - Swift

At the moment I have a simple UICollectionReusableView Class implemented on the CollectionView header cell:

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

I then add this to my UICollectionViewClass to show the header below:

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
}

How do i get access to the UISearchBar properties including delegates and text in my UICollectionViewClass ?

Upvotes: 0

Views: 2650

Answers (1)

avismara
avismara

Reputation: 5149

I am answering with the assumption that you have an instance of your SearchBarCollectionReusableView inside your UICollectionViewClass which too, under the assumption that it is your UIView extended class.

Suppose your SearchBarCollectionReusableView variable is searchBarCRV. Since all the instance level variables are public in Swift, you can directly access its instance variables. You can get the delegates with something like,

searchBarCRV.searchBar.delegate = self;

and text with...

var searchBarText = searchBarCRV.searchBar.text

... inside your UICollectionViewClass.

Upvotes: 1

Related Questions