目前我在 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/