iPhone/iPad 应用程序中的 UISearchBar
我有这样的数据...(所有数据都来自 .plist 文件...)
Searching Array - (
{
FirstName = "Ramesh";
LastName = "Bean";
EmpCode = 1001;
},
{
FirstName = "Rohan";
LastName = "Rathor";
EmpCode = 102;
},
{
FirstName = "Priya";
LastName = "Malhotra";
EmpCode = 103;
},
{
FirstName = "Mukesh";
LastName = "Sen";
EmpCode = 104;
},
{
FirstName = "Priya";
LastName = "Datta";
EmpCode = 105;
}
)
我想根据 FirstName (键)从该数组中实现搜索数据。
我可以使用“名字(键)”搜索数据,
但在过滤数据后假设我单击了 TableView 中显示的行(数据中)。它将我导航到新控制器,其中包含该特定员工的所有信息(例如:名字、姓氏、员工代码)。
我如何获取信息?
当我浏览搜索示例代码时。
这是我的搜索代码...
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
NSInteger TotalNoOfRecords=[self.SearchtableDataSource count];
for (int i=0;i<TotalNoOfRecords;i++)
{ NSDictionary *dictionary = [self.SearchtableDataSource objectAtIndex:i];
NSArray *array = [dictionary objectForKey:@"FirstName"];
[searchArray addObject:array];
}
for (NSString *sTemp in searchArray)
{
NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
{
[copyListOfItems addObject:sTemp];
}
}
我该如何改进此代码?...请指导我... [searchArray 发布];搜索数组 = nil;
我们如何维护 searchArray 中的所有“Keys(FirstName,LastName,EmpCode)”请帮助我?谢谢...
I have data like this...(All the data comes from .plist file...)
Searching Array - (
{
FirstName = "Ramesh";
LastName = "Bean";
EmpCode = 1001;
},
{
FirstName = "Rohan";
LastName = "Rathor";
EmpCode = 102;
},
{
FirstName = "Priya";
LastName = "Malhotra";
EmpCode = 103;
},
{
FirstName = "Mukesh";
LastName = "Sen";
EmpCode = 104;
},
{
FirstName = "Priya";
LastName = "Datta";
EmpCode = 105;
}
)
I want implement search data from this array on the basis of FirstName (key).
I am able to search data with the "FirstName(Key)"
but after filtering data suppose i clicked Row( in the data) which is displayed in the TableView. It Navigate me to New-Controller with all the information of that particular employee (like: FirstName,LastName,EmpCode).
How can i get information?
As i gone through the search sample codes.
Here is my search code...
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
NSInteger TotalNoOfRecords=[self.SearchtableDataSource count];
for (int i=0;i<TotalNoOfRecords;i++)
{ NSDictionary *dictionary = [self.SearchtableDataSource objectAtIndex:i];
NSArray *array = [dictionary objectForKey:@"FirstName"];
[searchArray addObject:array];
}
for (NSString *sTemp in searchArray)
{
NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
{
[copyListOfItems addObject:sTemp];
}
}
How can i improve this code?....Please guide me... [searchArray release]; searchArray = nil;
How we maintain all the "Keys(FirstName,LastName,EmpCode)" in the searchArray please help me out? Thanks...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为最好的方法是创建一个
NSDictionary
数组,它具有三个键的值,即 "FirstName"、"LastName"、< strong>“EmpCode”现在根据“FirstName”过滤数据使用 NSPredicate 而不是 for 循环,
在方法 cellForRowAtIndexPath 中
显示此 currentEmp 中的信息。
类似地,在
didSelectRowAtIndexPath
中,并将此字典传递到下一个 ViewController 中,您希望在其中显示当前员工的详细信息。
如果您有两个表视图,其中一个是
searchDisplayController
(显示过滤结果)一个显示整个结果,然后你可以通过一个 BOOL 变量跟踪来跟踪它,即过滤是否处于活动状态。
I think the best approach would be to make an array of
NSDictionary
which has value for three keys namely "FirstName", "LastName", "EmpCode"now to filter the data according to "FirstName" use NSPredicate instead of for loop,
In method cellForRowAtIndexPath
display the information in this currentEmp.
Similarly in
didSelectRowAtIndexPath
and pass this dictionary in the next ViewController in which u want to dispaly the detail of the current employ.
if u have two table views one that of
searchDisplayController
(showing the filtered result)and one which is showing the whole result, then u can track this by having a BOOL variable tacking that is if filtering is active or not.
这是我所做的,它对我来说工作得很好(测试过)。我已经采取了 UISearchBar 的一个 IBOutlet 所以这里是你的 .h 文件的代码
现在在 .m 文件中
出于例如目的我有一个包含 30 个字典的数组,每个字典都有一个唯一的名称。对于表视图数据源和委托... ..
现在就搜索而言,这就是您需要做的.......
我希望能帮助您理解这些事情,不要忘记在行中添加保留
filteredEmp = [[allEmpfilteredArrayUsingPredicate:predicate]retain];
因为方法
filteredArrayUsingPredicate:
是一个访问器方法,其保留计数由 NSArray 处理,因此可以访问过滤后的数组你需要向它传递一个保留消息。Here is what i have done and its working fine with me(tested it). I have taken one IBOutlet of UISearchBar so here is the code for ur .h file
Now in .m file
for e.g purpose i have thake an array of 30 Dictionaries, each with a unique name.For table view data source and delegate .....
Now as far as searching is concerned here is what u need to do .......
I hope that helps you understanding the things, dont forget to add retain in line
filteredEmp = [[allEmp filteredArrayUsingPredicate:predicate] retain];
because method
filteredArrayUsingPredicate:
is an accessor method and its retain count is handled by the NSArray so have access to the filtered array u need to pass a retain msg to it.