in your .h file declare the array name
@interface newactivecomposeViewController : UIViewController<
UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate>
{
BOOL SelectionAvailable;
}
@property (strong, nonatomic) NSMutableArray *AllDetails,*SuggestionArray;
@property (strong, nonatomic) UITableView *tablevie;
in your .m file
@synthesize SuggestionArray,AllDetails,tablevie;
- (void)viewDidLoad
{
SuggestionArray=[[NSMutableArray alloc]init]; //for using the searching
self.AllDetails=[[NSMutableArray alloc] init]; //store the all name in this array
tablevie=[[UITableView alloc]initWithFrame:CGRectMake(23, 136, 277, 214)]; //tableview created dynamically
tablevie.dataSource=self;
tablevie.delegate=self;
[self.tablevie registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
[self.view addSubview:tablevie];
NSString *substring = [NSString stringWithString:textField.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
return YES;
}
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {
[SuggestionArray removeAllObjects];
for (NSDictionary *tmp in self.AllDetails) {
NSRange substringRange =[[[tmp objectForKey:@"name"] lowercaseString] rangeOfString:[substring lowercaseString]];
if (substringRange.location==0) {
[SuggestionArray addObject:tmp];
}
}
if ([SuggestionArray count]==0) {
tablevie.hidden=YES;
}
else {
tablevie.hidden=NO;
}
[tablevie reloadData];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
for (NSString *tmp in getfrinendname) {
NSLog(@"%@", tmp);
if ([[textField.text lowercaseString] isEqualToString:[tmp lowercaseString]]) {
textField.text=tmp;
SelectionAvailable=YES;
break;
}
else {
SelectionAvailable=NO;
}
}
if (!SelectionAvailable) {
if (textField.text.length==0)
{
textField.text=@"";
}
// else if (textField.text.length >0)
// {
//
// [textField resignFirstResponder];
// [tablevie setHidden:YES];
//
// }
else
{
Alert=[[UIAlertView alloc]initWithTitle:@"User not found!" message:@"Please try again" delegate:Nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[Alert show];
}
}
#pragma tablevie data source
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"the count==%lu",(unsigned long)[SuggestionArray count]);
return [SuggestionArray count];
}
- (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] ;
}
cell.textLabel.text=[[SuggestionArray objectAtIndex:indexPath.row] objectForKey:@"name"]; //load your key or index
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
yourtextfieldname.text=[NSString stringWithFormat:@"%@",[[SuggestionArray objectAtIndex:indexPath.row] objectForKey:@"name"]];
}