Monday 23 January 2012

Search From TableView iPhone

////in .h file

:UITableviewController{

NSMutableArray *arrTableData;
    NSMutableArray *copyarrTableData;
    BOOL searching;
BOOL letUserSelectRow;
    
    IBOutlet UISearchBar *searchBar;

    OverlayViewController *ovController;
}
- (void) searchTableView;
- (void) doneSearching_Clicked:(id)sender;
@end

///in .m file
viewDidLoad{

//// JP - Searching....
    //Initialize the copy array.
copyarrTableData = [[NSMutableArray alloc] init];
    //Add the search bar
self.tableView.tableHeaderView = searchBar;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searching = NO;
letUserSelectRow = YES;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
if (searching)
return 1;
else
return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
    if (searching)
return [copyarrTableData count];
    else{
        return [arrTableData count];
    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"MyCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    // Configure the cell...
    if(searching) {
        
        objGetContInnerListData=[copyarrTableData objectAtIndex:indexPath.row];
        
        cell.textLabel.text=[NSString stringWithFormat:@"%@ %@",objGetContInnerListData.firstname,objGetContInnerListData.lastname];


    }
    else{
    objGetContInnerListData=[arrTableData objectAtIndex:indexPath.row];
    
    cell.textLabel.text=[NSString stringWithFormat:@"%@ %@",objGetContInnerListData.firstname,objGetContInnerListData.lastname];
//    cell.textLabel.text=objGetContInnerListData.firstname;
    }
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    
     ContactDetailView *detailViewController = [[ContactDetailView alloc] initWithNibName:@"ContactDetailView" bundle:nil];
    
    
    if(searching){
        
        objGetContInnerListData=[copyarrTableData objectAtIndex:indexPath.row];
        
        detailViewController.strautoid = objGetContInnerListData.autoid;
        detailViewController.objGetContInnerListData = objGetContInnerListData;
        [detailViewController.strautoid retain];
    }
    else{
        objGetContInnerListData=[arrTableData objectAtIndex:indexPath.row];
//        NSLog(@"\n\n DidSelect Obj Autoid : %@ %@ %@",objGetContactListData.autoid,objGetContactListData.clname,objGetContactListData.clid);
        detailViewController.objGetContInnerListData = objGetContInnerListData;
        detailViewController.strautoid = objGetContInnerListData.autoid;
        [detailViewController.strautoid retain]; 
    }
    [detailViewController.objGetContInnerListData retain];

    detailViewController.navigationItem.rightBarButtonItem = self.navigationItem.rightBarButtonItem;
    detailViewController.title = strtype;

     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
     
}

#pragma mark -
#pragma mark Search Bar 

- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar {
//This method is called again when the user clicks back from the detail view.
//So the overlay is displayed on the results, which is something we do not want to happen.
if(searching)
return;
//Add the overlay view.
if(ovController == nil)
ovController = [[OverlayViewController alloc] initWithNibName:@"OverlayView" bundle:[NSBundle mainBundle]];
CGFloat yaxis = self.navigationController.navigationBar.frame.size.height;
CGFloat width = self.view.frame.size.width;
CGFloat height = self.view.frame.size.height;
//Parameters x = origion on x-axis, y = origon on y-axis.
CGRect frame = CGRectMake(0, yaxis, width, height);
ovController.view.frame = frame;
ovController.view.backgroundColor = [UIColor grayColor];
ovController.view.alpha = 0.5;
ovController.rvController = self;
[self.tableView insertSubview:ovController.view aboveSubview:self.parentViewController.view];
searching = YES;
letUserSelectRow = NO;
self.tableView.scrollEnabled = NO;
//Add the done button.
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc
  initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
  target:self action:@selector(doneSearching_Clicked:)] autorelease];
}

- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {
    
//Remove all objects first.
[copyarrTableData removeAllObjects];
if([searchText length] > 0) {
[ovController.view removeFromSuperview];
searching = YES;
letUserSelectRow = YES;
self.tableView.scrollEnabled = YES;
[self searchTableView];
}
else {
[self.tableView insertSubview:ovController.view aboveSubview:self.parentViewController.view];
searching = NO;
letUserSelectRow = NO;
self.tableView.scrollEnabled = NO;
}
[self.tableView reloadData];
}

- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar {
[self searchTableView];
}

- (void) searchTableView {
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (GetContInnerListData *obj in arrTableData)
{
// NSArray *array = obj.firstname;//[dictionary valueForKey:@"firstname"];
// [searchArray addObject:[NSString stringWithFormat:@"%@ %@",obj.firstname,obj.lastname]];
//        [searchArray addObject:obj.firstname];
        NSComparisonResult result = [obj.lastname compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
[copyarrTableData addObject:obj];
}

}
// for (NSString *sTemp in searchArray)
// {
// NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
//
// if (titleResultsRange.length > 0)
// [copyarrTableData addObject:sTemp];
// }
[searchArray release];
searchArray = nil;
}

- (void) doneSearching_Clicked:(id)sender {
searchBar.text = @"";
[searchBar resignFirstResponder];
letUserSelectRow = YES;
searching = NO;
self.navigationItem.rightBarButtonItem = nil;
self.tableView.scrollEnabled = YES;
[ovController.view removeFromSuperview];
[ovController release];
ovController = nil;
[self.tableView reloadData];
}

/////overlay class with xib
take view based new file only

/// overlay.h file

#import <UIKit/UIKit.h>

@class ContactInnerListView;

@interface OverlayViewController : UIViewController {

ContactInnerListView *rvController;
}

@property (nonatomic, retain) ContactInnerListView *rvController;

@end

/// overlay.m file
#import "OverlayViewController.h"
#import "ContactInnerListView.h"

@implementation OverlayViewController

@synthesize rvController;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[rvController doneSearching_Clicked:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
[rvController release];
    [super dealloc];
}


@end





No comments:

Post a Comment