Skip to content

Instantly share code, notes, and snippets.

@quynguyen3490
Created January 4, 2015 09:50
Show Gist options
  • Select an option

  • Save quynguyen3490/c3fba2f65759b0408d4f to your computer and use it in GitHub Desktop.

Select an option

Save quynguyen3490/c3fba2f65759b0408d4f to your computer and use it in GitHub Desktop.
Search UI + Table View UI iOS
//
// ViewController.m
// SimpleTableView
//
// Created by Phi Long on 12/30/14.
// Copyright (c) 2014 Phi Long . All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController{
NSArray *table;
NSArray *searchResults;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
table= [NSArray arrayWithObjects:
@"Nguyễn Văn Quý",
@"Tang Tran Quoc Dat",
@"Nguyen Duc Anh",
@"Tran Loc",
@"Boi Quynh",
@"Nguyen Quoc Viet",
@"Phan Thi Kieu Diem",
@"Duong Phan Tra My",
@"Nguyen Thi Kim Anh",
@"Nguyen Thi Huong Sa",
@"Nguyen Thi Thanh Thanh", nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//khởi tạo số dòng table view
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(tableView == self.searchDisplayController.searchResultsTableView){
return [searchResults count];
}else{
return [table count];
}
}
//khởi tạo dữ liệu table view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIndentifer = @"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIndentifer];
if(cell==nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIndentifer];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [table objectAtIndex:indexPath.row];
}
return cell;
}
- (void)filterContentForSearchText:(NSString *)searchText scope:(NSString*)scope{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@",searchText];
searchResults = [table filteredArrayUsingPredicate:resultPredicate];
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString{
[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment