开发者

SearchBar programming in iPhone/iPad

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...


Use NSPredicate.

searchArray is your plist in a NSArray. Make a new searchArray2 from searchArray and then filter it using NSPredicate.

searchArray2 = [[NSMutableArray alloc] initWithArray:searchingArray copyItems:YES];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"FirstName IN %@", SearchtableDataSource];
[searchingArray2 filterUsingPredicate:predicate];

So your code should look like this:

tempArray = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"1" ofType:@"plist"]];
searchArray = [tempArray valueForKey:@"FirstName"];
NSString *searchText = searchBar.text;
NSMutableArray *searchResults;

for (NSString *sTemp in searchArray)
{
    NSComparisonResult result = [searchText compare:searchText options:(NSCaseInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
            if (result == NSOrderedSame)
            {
                [searchResults addObject:sTemp];
            }
searchArray2 = [[NSMutableArray alloc] initWithArray:searchArray copyItems:YES];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"FirstName IN %@", searchResults];
[searchArray2 filterUsingPredicate:predicate];
}

Then post searchArray2 to NSLOG
NSLOG(@"searchArray2: %@", searchArray2)
and tell me if you are satisfied with the results:)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜