我有一个UITextView,它已连接到UITableView以进行自动补全。

问题是表格显示格式不正确,与此相关的是以下问题:

  • 的详细信息不正确(例如:如果我按a,则不显示单词以第一个开头,它会按自己的喜好显示)。
  • 我的txt文件中有三种单词(例如Apple,A pple,A.pple);
    在我的表格中,如果我以字母“Ap”开始搜索,则仅显示A pple和A.pple,但不显示Apple,即使它显示A pple,直到我写“A P”,然后才停止显示单词。

  • 谁能让我知道该怎么做?

    请找到我的代码以供参考:

    很抱歉张贴所有代码,我正在做,因为我找不到它出了问题!
    - (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.
    }
    

    提前致谢!!!!

    最佳答案

    遵循此代码。U已正确完成所有操作,但搜索元素是问题所在。你已经写了代码。如果您只检查长度。如果其不为null,则将显示数据。但是您必须将第一个字母与元素数组进行比较。这样就可以正常工作。

    - (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];
        }
    }
    

    10-08 06:24