How To Auto Complete With Custom Values
来源:互联网 发布:修改系统启动顺序软件 编辑:程序博客网 时间:2024/06/06 03:44
When a user is entering data into a text field, it’s often nice to have the ability to automatically suggest completions for what they are entering, saving them time. For example, when you enter a URL into Safari, it will show you past URLs you have entered that you can select to save time.
Here’s how to implement this on the iPhone. First, you need to have a NSArray containing all of the data that you want to provide as potential options for the user. In this example, we’ll use an NSMutableArray of pastURLs, and every time the user browses to a URL we’ll add it to the array.
Next, we’ll need to create a view to display the URLs that the user can select from. One good way of doing this is just to create a table view below the input field that lists all of the potential options. This table view can appear only when the user is typing data into the text field, and can be hidden the rest of the time.
autocompleteTableView = [[UITableView alloc] initWithFrame: CGRectMake(0, 80, 320, 120) style:UITableViewStylePlain];autocompleteTableView.delegate = self;autocompleteTableView.dataSource = self;autocompleteTableView.scrollEnabled = YES;autocompleteTableView.hidden = YES; [self.view addSubview:autocompleteTableView];
Next, we need to know when the text field is being edited so we can display the table view, and make sure the appropriate elements are in the table view. A good way to get notification that the text field is being edited is by registering your view controller as a UITextFieldDelegate and implementing the shouldChangeCharactersInRange protocol. Here’s how we’ll do it:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { autocompleteTableView.hidden = NO; NSString *substring = [NSString stringWithString:textField.text]; substring = [substring stringByReplacingCharactersInRange:range withString:string]; [self searchAutocompleteEntriesWithSubstring:substring]; return YES;}
We want the table view to only show up the elements in our array that match the substring that the user has typed so far. So in our case we need to filter the pastURLs array by choosing the elements that start with the substring, and have the table view display those entries. Here’s how we’ve done this:
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring { // Put anything that starts with this substring into the autocompleteUrls array // The items in this array is what will show up in the table view [autocompleteUrls removeAllObjects]; for(NSString *curString in pastUrls) { NSRange substringRange = [curString rangeOfString:substring]; if (substringRange.location == 0) { [autocompleteUrls addObject:curString]; } } [autocompleteTableView reloadData];}
The table view just shows the contents of the autocompleteUrls, and that’s all there is to it! If you’d like to check this out for yourself, here’s a sample project.
- How To Auto Complete With Custom Values
- Auto Complete Tutorial for iOS: How To Auto Complete With Custom Values
- How to implement DynamicMBean with custom annotation
- How to sum values in an array with different hash
- QT18 how to link QListView with sqlite Database values
- QT19 How to link QComboBox with sqlite Database values
- How To Embedded StyleSheet File with Custom Control.
- XNA How To: Draw a Model with a Custom Effect
- HOW TO Load a Custom Script with Dependencies
- How to sort an NSMutableArray with custom objects in it?
- HOW TO Custom DSDT
- How to replace null values with immediately coming not null values
- How to Delete an Address from the Outlook Auto-Complete List
- Auto-complete javascript syntax with Vim
- Custom Auto-Generated Sequences with SQL Server
- Oracle query - how to make count to return values with 0
- FCKEditor 2.x: How to add a new button with custom javascript to the toolbar
- How to use isInEditMode() to see layout with custom View in the editor
- jQuery的优势
- hdu4155 The Game of 31------sg dfs
- vector,list,map,迭代器iterator
- poj 1034 The dog task
- Android中的SQLite使用学习
- How To Auto Complete With Custom Values
- bios内存分布图
- VB“懒人”函数
- 世界大学排行榜前100名
- UDP实现的多线程windows窗口对话
- 种子随机数问题及今天的作业
- apache/nginx access.log 说明
- 2012天津赛区网络赛 hdu 4280 Island Transport
- arguments 对象的 callee 属性