在 Tableview 中创建搜索字段

Posted

技术标签:

【中文标题】在 Tableview 中创建搜索字段【英文标题】:Create search field in Tableview 【发布时间】:2011-05-11 11:18:02 【问题描述】:

我正在创建一个带有表格视图的应用程序,其中包含大量数据。因此,我需要使用搜索字段。

有人知道如何在表格视图中创建搜索选项吗?

【问题讨论】:

你也应该标记你的编程语言。对于我的回答,我只是假设您使用的是 Objective - C... 在数组中搜索并在另一个数组中添加过滤的对象在表格视图中显示结果。在谷歌上搜索你会找到一个。 这里是苹果官方的例子developer.apple.com/library/ios/#samplecode/TableSearch/… 是的朋友,我在 Iphone 中使用目标 C 【参考方案1】:

使用 UISearchBar 属性,使用声明它

searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,30)];

并将其作为子视图添加到您的 UIViewController 的视图中。

之后,在视图控制器中使用搜索栏委托方法:

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar 
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar 
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar

查看this 教程,了解搜索栏及其代表!

编辑:此方法不是在 tableview 本身中使用搜索栏,而是在它之上。这意味着您必须将 tableView 作为 UIViewController 的子视图,并将 searchBar 作为同一 UIViewController 的子视图!

【讨论】:

谢谢我的朋友。它真的很棒。我还找到了 1 个文档,它和你的一样。它的链接是 iphonesdkarticles.com/2009/01/…。

以上是关于在 Tableview 中创建搜索字段的主要内容,如果未能解决你的问题,请参考以下文章

如何在 IOS 中创建具有不同 UITableViewCell 的 TableView?

如何在 tableView 中创建自定义单元格(滑动单元格)

SAP CRM 在Web UI中创建搜索帮助

如何在 coredata 中创建数据库视图

如何在 AngularJS 材料设计中创建简单的搜索输入文本?

如何在 swift 中在动态 TableView 中创建动态 tableView?