UITextField autocomplete

廉价感情. 提交于 2019-12-24 10:04:09

问题


Is it possible to autocomplete a UITextField bases on what is being entered? The reason why I need this is because I have a predefined set of words that the user needs to choose and the list is about 1000 item long. I want to display in UITableView and let the user chose but having 1k items in table list isn't I think a good idea. Thanks


回答1:


You can do it by not showing all the 1k items in table view but you can populate your table view as the user starts typing letters in text field. For this you have to put all the items in some data structure or database from where you can use some sorting or query to get the items related to what is user entered. For example if user starts typing letter from 'a' then get the items which have letter a and populate your table with them. One way of doing this is put all in database and in UITextField delegate

- (void)textFieldDidBeginEditing:(UITextField *)textField

start a NSTimer object which will call a function for every one second and that function will give you selected data from database, populate your table with that data.

Also you have to stop that timer in UITextField delegate -

- (BOOL)textFieldShouldReturn:(UITextField *)textField

or

-(void) textFieldDidEndEditing:(UITextField *)textField 



回答2:


You'll need to leverage TextFieldTextDidChange, as mentioned here: How can I get a textDidChange (like for a UISearchBar) method for a UITextField? but just how you present the available set, I'm not so sure...you could keep the table empty until X characters are entered and then search for matches to populate your (now smaller) table with



来源:https://stackoverflow.com/questions/6338900/uitextfield-autocomplete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!