//in .h
@interface SecondViewController : UIViewController<UISearchBarDelegate>{
NSMutableArray *nameStored;
IBOutlet UITableView *table;
NSMutableArray *searchResult;
NSArray *sections;
BOOL isSearchOn;
BOOL canSelectRow;
//IBOutlet UITableView *theTableView;
IBOutlet UISearchBar *searchBar;
}
- (void) doneSearching: (id)sender;
- (void) searchSoundsTableView;
//.m
- (void)viewDidLoad {
nameStored = [[NSMutableArray alloc] initWithObjects:
@"Aaron",
@"Abaddon",
@"Abana",
@"Abba",
@"Abdias",nil];
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searchResult = [[NSMutableArray alloc] init];
isSearchOn = NO;
canSelectRow = YES;
sections = [NSArray arrayWithObjects: @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil];
}
- (void) doneSearching:(id)sender {
isSearchOn = NO;
canSelectRow = YES;
//self.ta.scrollEnabled = YES;
//self.navigationItem.rightBarButtonItem = nil;
//---hides the keyboard---
[searchBar resignFirstResponder];
//---refresh the TableView---
//[self.theTableView reloadData];
[table reloadData];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[searchBar resignFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar1 {
// Clear the search text
// Deactivate the UISearchBar
searchBar1.text=@"";
[searchBar1 resignFirstResponder];
isSearchOn = NO;
canSelectRow = YES;
table.scrollEnabled = YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar1 {
[searchBar1 resignFirstResponder];
[self searchSoundsTableView];
}
- (void) searchSoundsTableView {
//---clears the search result---
[searchResult removeAllObjects];
for (NSString *str in nameStored)
{
//NSRange titleResultsRange = [str rangeOfString:searchBar.text options:NSCaseInsensitiveSearch];
NSRange titleResultsRange = [str rangeOfString:searchBar.text options:(NSAnchoredSearch | NSCaseInsensitiveSearch)];
if (titleResultsRange.length > 0)
[searchResult addObject:str];
}
}
- (NSIndexPath *)tableView :(UITableView *)theTableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (canSelectRow)
return indexPath;
else
return nil;
}
- (void)searchBar:(UISearchBar *)searchBar1 textDidChange:(NSString *)searchText {
[searchResult removeAllObjects];
//---if there is something to search for---
if ([searchText length] > 0) {
isSearchOn = YES;
canSelectRow = YES;
table.scrollEnabled = YES;
[self searchSoundsTableView];
}
else {
//---nothing to search---
isSearchOn = NO;
canSelectRow = NO;
table.scrollEnabled = YES;
}
[table reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (isSearchOn) {
return 1;
}
else
return [nameStored count];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
if (isSearchOn) {
NSArray*temp=[NSArray arrayWithObject:[[searchBar.text substringToIndex:1] uppercaseString]];
return temp;
}
else
return sections;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index {
if (isSearchOn) {
//NSUInteger indexOfTheObject = [self.sections indexOfObject:searchBar.text];
// return indexOfTheObject;
return 1;
}
else
return index;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (isSearchOn) {
return [[searchBar.text substringToIndex:1]uppercaseString];
}
else
{
return [sections objectAtIndex:section];
}
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
if (isSearchOn) {
NSArray *sectionArray = [searchResult filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [[searchBar.text substringToIndex:1]uppercaseString]]];
return [sectionArray count];
} else
{
NSArray *sectionArray = [nameStored filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [sections objectAtIndex:section]]];
return [sectionArray count];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 35.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
if (isSearchOn) {
NSString *cellValue = [searchResult objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
} else {
NSArray *sectionArray = [nameStored filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [sections objectAtIndex:indexPath.section]]];
NSString *temp = [sectionArray objectAtIndex:indexPath.row];
cell.textLabel.text = temp;
//cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
//cell.accessoryType = UITableViewCellAccessoryCheckmark;
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
@interface SecondViewController : UIViewController<UISearchBarDelegate>{
NSMutableArray *nameStored;
IBOutlet UITableView *table;
NSMutableArray *searchResult;
NSArray *sections;
BOOL isSearchOn;
BOOL canSelectRow;
//IBOutlet UITableView *theTableView;
IBOutlet UISearchBar *searchBar;
}
- (void) doneSearching: (id)sender;
- (void) searchSoundsTableView;
//.m
- (void)viewDidLoad {
nameStored = [[NSMutableArray alloc] initWithObjects:
@"Aaron",
@"Abaddon",
@"Abana",
@"Abba",
@"Abdias",nil];
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searchResult = [[NSMutableArray alloc] init];
isSearchOn = NO;
canSelectRow = YES;
sections = [NSArray arrayWithObjects: @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil];
}
- (void) doneSearching:(id)sender {
isSearchOn = NO;
canSelectRow = YES;
//self.ta.scrollEnabled = YES;
//self.navigationItem.rightBarButtonItem = nil;
//---hides the keyboard---
[searchBar resignFirstResponder];
//---refresh the TableView---
//[self.theTableView reloadData];
[table reloadData];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[searchBar resignFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar1 {
// Clear the search text
// Deactivate the UISearchBar
searchBar1.text=@"";
[searchBar1 resignFirstResponder];
isSearchOn = NO;
canSelectRow = YES;
table.scrollEnabled = YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar1 {
[searchBar1 resignFirstResponder];
[self searchSoundsTableView];
}
- (void) searchSoundsTableView {
//---clears the search result---
[searchResult removeAllObjects];
for (NSString *str in nameStored)
{
//NSRange titleResultsRange = [str rangeOfString:searchBar.text options:NSCaseInsensitiveSearch];
NSRange titleResultsRange = [str rangeOfString:searchBar.text options:(NSAnchoredSearch | NSCaseInsensitiveSearch)];
if (titleResultsRange.length > 0)
[searchResult addObject:str];
}
}
- (NSIndexPath *)tableView :(UITableView *)theTableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (canSelectRow)
return indexPath;
else
return nil;
}
- (void)searchBar:(UISearchBar *)searchBar1 textDidChange:(NSString *)searchText {
[searchResult removeAllObjects];
//---if there is something to search for---
if ([searchText length] > 0) {
isSearchOn = YES;
canSelectRow = YES;
table.scrollEnabled = YES;
[self searchSoundsTableView];
}
else {
//---nothing to search---
isSearchOn = NO;
canSelectRow = NO;
table.scrollEnabled = YES;
}
[table reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (isSearchOn) {
return 1;
}
else
return [nameStored count];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
if (isSearchOn) {
NSArray*temp=[NSArray arrayWithObject:[[searchBar.text substringToIndex:1] uppercaseString]];
return temp;
}
else
return sections;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index {
if (isSearchOn) {
//NSUInteger indexOfTheObject = [self.sections indexOfObject:searchBar.text];
// return indexOfTheObject;
return 1;
}
else
return index;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (isSearchOn) {
return [[searchBar.text substringToIndex:1]uppercaseString];
}
else
{
return [sections objectAtIndex:section];
}
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
if (isSearchOn) {
NSArray *sectionArray = [searchResult filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [[searchBar.text substringToIndex:1]uppercaseString]]];
return [sectionArray count];
} else
{
NSArray *sectionArray = [nameStored filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [sections objectAtIndex:section]]];
return [sectionArray count];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 35.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
if (isSearchOn) {
NSString *cellValue = [searchResult objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
} else {
NSArray *sectionArray = [nameStored filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [sections objectAtIndex:indexPath.section]]];
NSString *temp = [sectionArray objectAtIndex:indexPath.row];
cell.textLabel.text = temp;
//cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
//cell.accessoryType = UITableViewCellAccessoryCheckmark;
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
No comments:
Post a Comment