UITextView自动完成文本不我订购

我有一个连接到UITableView自动完成的UITextView

问题是表格显示格式不正确,我有以下问题:

  1. 细节不按顺序(例如:如果我按一个不显示的话,首先,它显示,因为它喜欢)。

  2. 我在我的txt文件中有三种单词(如Apple,A pple,A.pple); 在我的表中,它只显示一个pple和apple,但不是苹果,如果我开始用字母'ap'search,即使它显示一个pple直到我写'AP',然后停止显示这个单词。

任何人都可以让我知道该怎么做?

请find我的代码供您参考:

对不起发布所有的代码,我在做,因为我找不到它是错误的!

 - (void) finishedSearching { [usernameField resignFirstResponder]; autoCompleteTableView.hidden = YES; } - (void)viewDidLoad { NSString *filePath = [[NSBundle mainBundle] pathForResource:@"employee.txt" ofType:nil]; NSData* data = [NSData dataWithContentsOfFile:filePath]; //Convert the bytes from the file into a string NSString* string = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding]; //Split the string around newline characters to create an array NSString* delimiter = @"\n"; NSArray *item = [string componentsSeparatedByString:delimiter]; elementArray = [[NSMutableArray alloc] initWithArray:item]; usernameField = [[UITextField alloc] initWithFrame:CGRectMake(204, 405, 264, 31)]; usernameField.borderStyle = 3; // rounded, recessed rectangle usernameField.autocorrectionType = UITextAutocorrectionTypeNo; usernameField.autocapitalizationType = UITextAutocapitalizationTypeNone; usernameField.textAlignment = UITextAlignmentLeft; usernameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; usernameField.returnKeyType = UIReturnKeyDone; usernameField.font = [UIFont fontWithName:@"Trebuchet MS" size:20]; usernameField.textColor = [UIColor blackColor]; usernameField.placeholder=@"Login id"; [usernameField setDelegate:self]; [self.view addSubview:usernameField]; autoCompleteArray = [[NSMutableArray alloc] init]; autoCompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(204, 450, 264, tableHeight) style:UITableViewStylePlain]; autoCompleteTableView.delegate = self; autoCompleteTableView.dataSource = self; autoCompleteTableView.scrollEnabled = YES; autoCompleteTableView.hidden = YES; autoCompleteTableView.rowHeight = tableHeight; [self.view addSubview:autoCompleteTableView]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring { // Put anything that starts with this substring into the autoCompleteArray // The items in this array is what will show up in the table view [autoCompleteArray removeAllObjects]; for(NSString *curString in elementArray) { NSRange substringRangeLowerCase = [curString rangeOfString:[substring lowercaseString]]; NSRange substringRangeUpperCase = [curString rangeOfString:[substring uppercaseString]]; if (substringRangeLowerCase.length != 0 || substringRangeUpperCase.length != 0) { [autoCompleteArray addObject:curString]; } } autoCompleteTableView.hidden = NO; [autoCompleteTableView reloadData]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; [super touchesBegan:touches withEvent:event]; [self finishedSearching]; } #pragma mark UITextFieldDelegate methods // Close keyboard when Enter or Done is pressed - (BOOL)textFieldShouldReturn:(UITextField *)textField { BOOL isDone = YES; if (isDone) { [self finishedSearching]; return YES; } else { return NO; } } // String in Search textfield - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSString *substring = [NSString stringWithString:textField.text]; substring = [substring stringByReplacingCharactersInRange:range withString:string]; [self searchAutocompleteEntriesWithSubstring:substring]; return YES; } #pragma mark UITableViewDelegate methods - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section { //Resize auto complete table based on how many elements will be displayed in the table if (autoCompleteArray.count >=3) { autoCompleteTableView.frame = CGRectMake(204, 450, 264, tableHeight*3); return autoCompleteArray.count; } else if (autoCompleteArray.count == 2) { autoCompleteTableView.frame = CGRectMake(204, 450, 264, tableHeight*2); return autoCompleteArray.count; } else { autoCompleteTableView.frame = CGRectMake(204, 450, 264, tableHeight); return autoCompleteArray.count; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = nil; static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier"; cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] ; } cell.textLabel.text = [autoCompleteArray objectAtIndex:indexPath.row]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; usernameField.text = selectedCell.textLabel.text; usernameField.text=[usernameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; NSLog(@"%@",usernameField.text); [self finishedSearching]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } 

提前致谢!!!!

按照这个代码..你已经做得正确,但search元素是问题。 你写的代码。 如果你单独检查长度。 如果它不为空,它将显示数据。 但是你必须比较第一个字母和元素数组。 所以它工作正常。

 - (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring { [autocompleteArray removeAllObjects]; for(NSString *curString in elementArray) { NSRange substringRange = [curString rangeOfString:substring]; if (substringRange.location == 0) { [autocompleteArray addObject:curString]; } [autocompleteTableView reloadData]; } }