Last active
August 29, 2015 14:23
-
-
Save anzfactory/a808427c1ebff522ec83 to your computer and use it in GitHub Desktop.
UISearchBarにリストをくっつけてみた...なんか標準機能だけで出来そうな気もするけど
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ListSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate { | |
var myDelegate: UISearchBarDelegate? = nil | |
lazy var tableView: UITableView = self.setupTableView() | |
private var items = [String]() | |
private var isShowList = false | |
private var listHeight: CGFloat = 0.0 | |
// delegate設定を奪う | |
// ほんちゃんは自分でつかいたいんで | |
override var delegate: UISearchBarDelegate? { | |
set { | |
myDelegate = newValue | |
} | |
get { | |
return myDelegate | |
} | |
} | |
required init(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
setup(); | |
} | |
override func didMoveToSuperview() { | |
if let s = self.superview { | |
s.addSubview(self.tableView) | |
} else { | |
self.tableView.removeFromSuperview() | |
} | |
} | |
deinit { | |
NSNotificationCenter.defaultCenter().removeObserver(self) | |
} | |
func setup() { | |
NSNotificationCenter.defaultCenter().addObserver( | |
self, | |
selector: "receiveKeyboardWillShowNotification:", | |
name: UIKeyboardWillShowNotification, | |
object: nil | |
) | |
NSNotificationCenter.defaultCenter().addObserver( | |
self, | |
selector: "receiveKeyboardWillHideNotification:", | |
name: UIKeyboardWillHideNotification, | |
object: nil | |
) | |
super.delegate = self | |
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") | |
self.tableView.dataSource = self | |
self.tableView.delegate = self | |
} | |
func setupTableView() -> UITableView { | |
let tableView = UITableView( | |
frame: CGRect( | |
x:self.frame.origin.x, | |
y:self.frame.origin.y + self.frame.size.height, | |
width:self.frame.size.width, | |
height:0), | |
style: UITableViewStyle.Plain | |
) | |
return tableView | |
} | |
func setList(items: [String]) { | |
self.items.removeAll(keepCapacity: false) | |
for s in items { | |
self.items.append(s) | |
} | |
} | |
func showList() { | |
if self.isShowList || self.listHeight <= 0 { | |
return | |
} | |
self.superview?.bringSubviewToFront(self.tableView) | |
self.isShowList = true | |
self.tableView.reloadData() | |
self.tableView.frame = CGRect( | |
x: self.frame.origin.x, | |
y: self.frame.origin.y + self.frame.size.height, | |
width: self.frame.size.width, | |
height: self.listHeight | |
) | |
self.tableView.scrollRectToVisible(CGRect.zeroRect, animated: false) | |
} | |
func hideList() { | |
if !self.isShowList { | |
return | |
} | |
self.isShowList = false | |
self.tableView.frame = CGRect( | |
x: self.frame.origin.x, | |
y: self.frame.origin.y + self.frame.size.height, | |
width: self.frame.size.width, | |
height: 0 | |
) | |
} | |
// MARK: UITableViewDataSource | |
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return self.items.count | |
} | |
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell | |
cell.textLabel?.text = self.items[indexPath.row] | |
return cell | |
} | |
// MARK: UITableViewDelegate | |
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { | |
return 44 | |
} | |
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { | |
self.text = self.items[indexPath.row] | |
self.hideList() | |
} | |
// MARK: NSNotification | |
func receiveKeyboardWillShowNotification(notification: NSNotification) { | |
if self.isFirstResponder() { | |
var h = notification.userInfo![UIKeyboardFrameEndUserInfoKey]!.CGRectValue().size.height | |
self.listHeight = (self.superview?.frame.size.height ?? 0) - h | |
showList() | |
} | |
} | |
func receiveKeyboardWillHideNotification(notification: NSNotification) { | |
hideList() | |
} | |
// MARK: UISearchBarDelegate | |
func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool { | |
return self.myDelegate?.searchBarShouldBeginEditing?(searchBar) ?? true | |
} | |
func searchBarTextDidBeginEditing(searchBar: UISearchBar) { | |
showList() | |
self.myDelegate?.searchBarTextDidBeginEditing?(searchBar) | |
} | |
func searchBarShouldEndEditing(searchBar: UISearchBar) -> Bool { | |
return self.myDelegate?.searchBarShouldEndEditing?(searchBar) ?? true | |
} | |
func searchBarTextDidEndEditing(searchBar: UISearchBar) { | |
self.myDelegate?.searchBarTextDidBeginEditing?(searchBar) | |
} | |
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { | |
if !self.isShowList { | |
showList() | |
} | |
self.myDelegate?.searchBar?(searchBar, textDidChange: searchText) | |
} | |
func searchBar(searchBar: UISearchBar, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { | |
return self.myDelegate?.searchBar?(searchBar, shouldChangeTextInRange: range, replacementText: text) ?? true | |
} | |
func searchBarSearchButtonClicked(searchBar: UISearchBar) { | |
hideList() | |
self.myDelegate?.searchBarSearchButtonClicked?(searchBar) | |
} | |
func searchBarBookmarkButtonClicked(searchBar: UISearchBar) { | |
self.myDelegate?.searchBarBookmarkButtonClicked?(searchBar) | |
} | |
func searchBarCancelButtonClicked(searchBar: UISearchBar) { | |
self.myDelegate?.searchBarCancelButtonClicked?(searchBar) | |
} | |
func searchBarResultsListButtonClicked(searchBar: UISearchBar) { | |
self.myDelegate?.searchBarResultsListButtonClicked?(searchBar) | |
} | |
func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) { | |
self.myDelegate?.searchBar?(searchBar, selectedScopeButtonIndexDidChange: selectedScope) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment