UITextView自动完成文本不是我订购的

UITextView自动完成文本不是我订购的

本文介绍了UITextView自动完成文本不是我订购的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITextView ,它连接到 UITableView 以进行自动完成。

I have a UITextView which is connected to UITableView for Autocompletion.

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

The problem is the table display is not properly formatted, i have following problems with this:


  1. 细节不按顺序排序(例如:如果按下它不显示单词以第一个开头,则显示为喜欢的单词)。

  1. the details are not in order(ex: if i press a it is not displaying words starts with a first, it display as it likes).

我有三个我的txt文件中的一些单词(如Apple,A pple,A.pple);
在我的表格中它只显示A pple和A.pple而不是Apple如果我开始搜索字母'Ap',即使它显示A pple直到我写'A P'然后它停止显示单词。

i have three kind of words in my txt file(like Apple, A pple, A.pple);in my table it display only A pple and A.pple but not Apple if i start search with letter 'Ap' even it displays A pple till i write 'A P' then it stops displaying the words.

任何人都可以告诉我该怎么做吗?

Can any one let me know what to do that?

请找我的代码供您参考:

Please find my code for your reference:

很抱歉发布所有代码,我这样做是因为我找不到它出错了!!!

Sorry for posting all the code, i am doing because i cannot find were its going wrong!!!

- (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.
}

提前致谢!!!!

推荐答案

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

follow this code.. U have done all correctly but searching the element is the problem. U have written the code. if your checking length alone. if its not null it will show data. but u have to compare the first letter with element array. so that it works fine.

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

这篇关于UITextView自动完成文本不是我订购的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 03:38