3

我有一个用于自动完成UITextView的连接。UITableView

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

  1. 详细信息不按顺序排列(例如:如果我按 a 它不显示以第一个开头的单词,它会随意显示)。

  2. 我的 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.
}

提前致谢!!!!

4

1 回答 1

2

遵循此代码..您已正确完成所有操作,但搜索元素是问题所在。你已经写了代码。如果您单独检查长度。如果它不为空,它将显示数据。但是您必须将第一个字母与元素数组进行比较。使其正常工作。

- (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];
    }
}
于 2012-07-06T05:40:26.713 回答