gpt4 book ai didi

iphone - UITextView 自动完成不是我订单的文本

转载 作者:行者123 更新时间:2023-12-03 19:55:12 25 4
gpt4 key购买 nike

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

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

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

  2. 我的 txt 文件中有三种单词(例如 Apple、A pple、A.pple);在我的表格中,如果我用字母“Ap”开始搜索,它只显示 A pple 和 A.apple,但不会显示 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.
}

提前致谢!!!

最佳答案

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

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

关于iphone - UITextView 自动完成不是我订单的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11344461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com