开发者

how to implement search functionality on tableview in iphone

i have a tableview. i loaded the rules into the table of search view controller. when i type character into searchbar how to implement search functionality and how to add it to the rulesArray. i have an array 'rulesArray" with all rules like below.

[rulesArray addObject:[rules objectAtIndex:k]];

Rule is a class. Also I want to add rule type to the rulesArray.

How to do both issues?

Code is below:

Rule *bRule;
for(bRule in rulesArray)
    {
        NSString *name=(NSString *) bTopic;
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
        NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];
        if(r.location != NSNotFound)
        {
            if(r.location== 0)//that is we ar开发者_StackOverflow中文版e checking only the start of the names.
            {
                [rulesArray addObject:name];
            }

        }


You need 2 arrays. One with ALL of your rules and one for the searchbar.

if the searchbar is empty, copy all items of the "full"-array to the "search"-array.

the searchbarArray is connected with the tableView, always! the fullArray exists just for searching in it.

-(void)search:(NSString*)searchTerm
{
    [searchArray removeAllObjects];
    Rule *bRule;
    for(bRule in rulesArray)
    {
       //if bRule contains "searchTerm" add it to the searchArray;
    }
    [tableView reloadData];
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜