Use of iOS UISearchController

Source: Internet
Author: User

Use of iOS UISearchController
-(Void) viewWillAppear :( BOOL) animated {
[Super viewWillAppear: animated];
[Self setTabBarHidden: YES withAnimation: YES];
}
-(Void) viewWillDisappear :( BOOL) animated {
[Super viewWillDisappear: animated];
[Self. mySearchController. searchBar setHidden: YES];
[Self setTabBarHidden: NO withAnimation: YES];
}
-(Void) viewDidLoad {
[Super viewDidLoad];
[Self setExtraCellLineHidden: self. myTableViewW
];
[Self initCellInfo];
[Self initTableView];
[Self initTitleBar];
[Self initHeader];
[Self initSearchBar];
}
-(Void) initSearchBar {
Self. mySearchController = [[UISearchController alloc] initWithSearchResultsController: nil];
Self. mySearchController. searchBar. frame = CGRectMake (100, 10,200, 44 );
Self. myTableViewW. tableHeaderView = self. mySearchController. searchBar;
[Self. mySearchController. searchBar sizeToFit];
Self. mySearchController. searchResultsUpdater = self;
Self. mySearchController. dimsBackgroundDuringPresentation = NO;
Self. mySearchController. hidesNavigationBarDuringPresentation = NO;
Self. mySearchController. searchBar. placeholder = @ "Enter the ID or nickname of the rabbit to query ";
[Self presentViewController: self. mySearchController animated: YES completion: nil];
}
-(Void) initHeader {
}
-(BOOL) textFieldShouldReturn :( UITextField *) textField {
[TextField resignFirstResponder];
Return YES;
}
-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {
[Self hiddenkeyBoard];
}
-(Void) hiddenkeyBoard {
[Self. myTextField resignFirstResponder];
}
-(Void) initTitleBar {
Self. title = @ "add a friend ";
}
-(Void) initCellInfo {
NSMutableArray * cellInfoArray = [[NSMutableArray alloc] init];
NSMutableArray * nickNameArray = [[NSMutableArray alloc] init];
NSMutableArray * IDArray = [[NSMutableArray alloc] init];
CellInfo_t * cellInfo = [self newCellInfo];
CellInfo. image = @ "http:// B .hiphotos.baidu.com/image/w%3D310/sign=a0eb5ed6d52a60595210e71b1835342d/2fdda3cc7cd98d10f00dbe2e233fb80e7aec90d0.jpg ";
CellInfo. nickName = @ "";
CellInfo. ID = @ "1 ";
[CellInfoArray addObject: cellInfo];
[NickNameArray addObject: cellInfo. nickName];
[IDArray addObject: cellInfo. ID];

CellInfo = [self newCellInfo];
CellInfo. image = @ "http://c.hiphotos.baidu.com/image/w%3D310/sign=4632f6f379cb0a4685228d385b62f63e/902397dda144ad341343938ed2a20cf431ad853e.jpg ";
CellInfo. nickName = @ "Ren dissolve ";
CellInfo. ID = @ "2 ";
[CellInfoArray addObject: cellInfo];
[NickNameArray addObject: cellInfo. nickName];
[IDArray addObject: cellInfo. ID];

CellInfo = [self newCellInfo];
CellInfo. image = @ "http://tao.goulew.com/users/upfile/201403/20140305222531big.jpg ";
CellInfo. nickName = @ "Xiao anghu ";
CellInfo. ID = @ "3 ";
[CellInfoArray addObject: cellInfo];
[NickNameArray addObject: cellInfo. nickName];
[IDArray addObject: cellInfo. ID];

CellInfo = [self newCellInfo];
CellInfo. image = @ "http://d.hiphotos.baidu.com/image/w%3D310/sign=4276550c5e6034a829e2be80fb1249d9/d000baa1cd11728b9c43502fcafcc3cec3fd2c55.jpg ";
CellInfo. nickName = @ "poor roads ";
CellInfo. ID = @ "4 ";
[CellInfoArray addObject: cellInfo];
[NickNameArray addObject: cellInfo. nickName];
[IDArray addObject: cellInfo. ID];

Self. nickNameArray = nickNameArray;
Self. cellInfoArray = cellInfoArray;
Self. IDArray = IDArray;
}
-(CellInfo_t *) newCellInfo {
CellInfo_t * info = [CellInfo_t new];
Return info;
}
-(Void) initTableView {
Self. myTableViewW. delegate = self;
Self. myTableViewW. dataSource = self;
}
-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {
Return 1;
}
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {
If (self. mySearchController. active ){
Return self. searchList. count;
} Else {
Return self. cellInfoArray. count;
}
}
-(CellInfo_t *) getCellInfo :( NSInteger) row {
Return self. cellInfoArray [row];
}
-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {
Return 80;
}
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {
CellInfo_t * cellInfo = [self getCellInfo: indexPath. row];
Static NSString * cellID = @ "RRAddFriendCell ";
RRAddFriendCell * cell = [tableView dequeueReusableCellWithIdentifier: cellID];
If (cell = nil ){
Cell = [[[NSBundle mainBundle] loadNibNamed: cellID owner: self options: nil] lastObject];
}
If (self. mySearchController. active ){
Cell. mynickName = self. searchList [indexPath. row];
} Else {
PAImageView * image = [[PAImageView alloc] initWithFrame: cell. myimage. bounds backgroundProgressColor: [UIColor lightGrayColor] progressColor: nil image: nil];
[Cell. myimage addSubview: image];
[Image setImageURL: cellInfo. image];

Cell. mynickName = cellInfo. nickName;
Cell. myID = cellInfo. ID;
}
[Cell. myapplyAdd setTitle: @ "apply to add" forState: UIControlStateNormal];
[Cell. myapplyAdd addTarget: self action: @ selector (applyAdd) forControlEvents: UIControlEventTouchUpInside];
Cell. selectionStyle = UITableViewCellSelectionStyleNone;
Return cell;
}
-(BOOL) searchBarShouldBeginEditing :( UISearchBar *) searchBar {
Return YES;
}
-(Void) updateSearchResultsForSearchController :( UISearchController *) searchController {
NSString * searchString = self. mySearchController. searchBar. text;
NSPredicate * preicate = [NSPredicate predicateWithFormat: @ "self contains [c] % @", searchString]; // use predicateWithFormat to create a predicateWithFormat. name is used as the Key Path.
If (self. searchList! = Nil ){
[Self. searchList removeAllObjects];
}
// Filter data
Self. searchList = [NSMutableArray arrayWithArray: [self. nickNameArray filteredArrayUsingPredicate: preicate];
[Self. myTableViewW reloadData];
}
-(Void) applyAdd {
NSLog (@ "apply to add ");
}
-(Void) setExtraCellLineHidden: (UITableView *) tableView {
UIView * view = [[UIView alloc] init];
View. backgroundColor = [UIColor clearColor];
[TableView setTableFooterView: view];
[TableView setTableHeaderView: view];
}
-(BOOL) searchBarShouldEndEditing :( UISearchBar *) searchBar {
[SearchBar endEditing: YES];
Return YES;
}
-(Void) searchBarSearchButtonClicked :( UISearchBar *) searchBar {
[SearchBar endEditing: YES];
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.