UITextField自动完成 [英] UITextField autocomplete

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

问题描述

是否可以根据输入的内容自动完成UITextField?我之所以需要它,是因为我有用户需要选择的一组预定义单词,并且列表的长度约为1000个项目。我想在UITableView中显示并让用户选择,但是在表列表中包含1k项并不是一个好主意。
谢谢

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

推荐答案

您可以通过不在表格视图中显示所有1k项目来做到这一点,但是可以填充表格当用户开始在文本字段中输入字母时,视图。为此,您必须将所有项目放置在某种数据结构或数据库中,从中可以使用某种排序或查询来获取与用户输入内容相关的项目。例如,如果用户开始从 a输入字母,则获取字母为a的项目,并在其中填充表格。一种方法是将全部放入数据库和 UITextField 委托

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

启动 NSTimer 对象,该对象每秒调用一次函数,该函数将为您提供从数据库中选择的数据,并使用该数据填充表。

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.

另外,您还必须在 UITextField 委托中停止该计时器-

Also you have to stop that timer in UITextField delegate -

- (BOOL)textFieldShouldReturn:(UITextField *)textField

or

-(void) textFieldDidEndEditing:(UITextField *)textField 

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆