iPhone/iPad 应用程序中的 UISearchBar

发布于 2024-10-17 22:59:31 字数 1733 浏览 4 评论 0原文

我有这样的数据...(所有数据都来自 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

じ违心 2024-10-24 22:59:31

我认为最好的方法是创建一个 NSDictionary 数组,它具有三个键的值,即 "FirstName""LastName"、< strong>“EmpCode”

现在根据“FirstName”过滤数据使用 NSPredicate 而不是 for 循环,

NSString *searchText = searchBar.text;

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"FirstName like[cd] %@",searchText];
NSArray* filteredArray = [self.SearchtableDataSource filteredArrayUsingPredicate:predicate];

在方法 cellForRowAtIndexPath 中

NSDictionary* currentEmp = [filteredArray objectAtIndex:inddexPath.row];

显示此 currentEmp 中的信息。

类似地,在 didSelectRowAtIndexPath

NSDictionary* currentEmp = [filteredArray objectAtIndex:inddexPath.row];

,并将此字典传递到下一个 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,

NSString *searchText = searchBar.text;

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"FirstName like[cd] %@",searchText];
NSArray* filteredArray = [self.SearchtableDataSource filteredArrayUsingPredicate:predicate];

In method cellForRowAtIndexPath

NSDictionary* currentEmp = [filteredArray objectAtIndex:inddexPath.row];

display the information in this currentEmp.

Similarly in didSelectRowAtIndexPath

NSDictionary* currentEmp = [filteredArray objectAtIndex:inddexPath.row];

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.

败给现实 2024-10-24 22:59:31

这是我所做的,它对我来说工作得很好(测试过)。我已经采取了 UISearchBar 的一个 IBOutlet 所以这里是你的 .h 文件的代码

@interface SearchForEmp : UIViewController<'UISearchDisplayDelegate,UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource> {

IBOutlet UISearchBar* mySearchBar;
UISearchDisplayController* mySearchDisplayController;

NSMutableArray* allEmp;
NSMutableArray* filteredEmp;
BOOL isFilatering;
}
@end

现在在 .m 文件中

-(void)viewDidLoad 
{
[super viewDidLoad];

mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:mySearchBar contentsController:self];
mySearchDisplayController.searchResultsDelegate = self;
mySearchDisplayController.searchResultsDataSource = self;
mySearchDisplayController.delegate = self;

isFilatering = NO;
filteredEmp = [[NSMutableArray alloc] init];
allEmp = [[NSMutableArray alloc] init];
NSDictionary *temp;
for (int i = 0; i < 30; i++) {
    temp = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"FirstName_%d",i],@"FirstName",[NSString stringWithFormat:@"LastName_%d",i],@"LastName",[NSNumber numberWithInt:1000+i],@"EmpCode",nil];
    [allEmp addObject:temp];
}
}

出于例如目的我有一个包含 30 个字典的数组,每个字典都有一个唯一的名称。对于表视图数据源和委托... ..

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

if (isFilatering) {
    return [filteredEmp count];
}
else {
    return [allEmp count];
}
return [allEmp count];

}




-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *CellIdentifier = @"Cell";

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] init] autorelease];
}

NSDictionary* temp;
if (isFilatering) {
    temp = [filteredEmp objectAtIndex:indexPath.row];
}
else {
    temp = [allEmp objectAtIndex:indexPath.row];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",[temp objectForKey:@"FirstName"],[temp objectForKey:@"LastName"]];
return cell;
}

现在就搜索而言,这就是您需要做的.......

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText

{

NSPredicate* predicate = [[NSPredicate predicateWithFormat:@"self.FirstName contains %@",mySearchBar.text] retain];
filteredEmp = [[allEmp filteredArrayUsingPredicate:predicate] retain];

}

-(void) searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller

{

isFilatering = YES;

}

-(void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller

{

isFilatering = NO;

}

我希望能帮助您理解这些事情,不要忘记在行中添加保留
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

@interface SearchForEmp : UIViewController<'UISearchDisplayDelegate,UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource> {

IBOutlet UISearchBar* mySearchBar;
UISearchDisplayController* mySearchDisplayController;

NSMutableArray* allEmp;
NSMutableArray* filteredEmp;
BOOL isFilatering;
}
@end

Now in .m file

-(void)viewDidLoad 
{
[super viewDidLoad];

mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:mySearchBar contentsController:self];
mySearchDisplayController.searchResultsDelegate = self;
mySearchDisplayController.searchResultsDataSource = self;
mySearchDisplayController.delegate = self;

isFilatering = NO;
filteredEmp = [[NSMutableArray alloc] init];
allEmp = [[NSMutableArray alloc] init];
NSDictionary *temp;
for (int i = 0; i < 30; i++) {
    temp = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"FirstName_%d",i],@"FirstName",[NSString stringWithFormat:@"LastName_%d",i],@"LastName",[NSNumber numberWithInt:1000+i],@"EmpCode",nil];
    [allEmp addObject:temp];
}
}

for e.g purpose i have thake an array of 30 Dictionaries, each with a unique name.For table view data source and delegate .....

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

if (isFilatering) {
    return [filteredEmp count];
}
else {
    return [allEmp count];
}
return [allEmp count];

}




-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *CellIdentifier = @"Cell";

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] init] autorelease];
}

NSDictionary* temp;
if (isFilatering) {
    temp = [filteredEmp objectAtIndex:indexPath.row];
}
else {
    temp = [allEmp objectAtIndex:indexPath.row];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",[temp objectForKey:@"FirstName"],[temp objectForKey:@"LastName"]];
return cell;
}

Now as far as searching is concerned here is what u need to do .......

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText

{

NSPredicate* predicate = [[NSPredicate predicateWithFormat:@"self.FirstName contains %@",mySearchBar.text] retain];
filteredEmp = [[allEmp filteredArrayUsingPredicate:predicate] retain];

}

-(void) searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller

{

isFilatering = YES;

}

-(void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller

{

isFilatering = NO;

}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文