Search


UISearchBar  &  UISearchBarDelegate
Demo中的代码 - - ,了解下思路


http://blog.sina.com.cn/s/blog_67c938a20100v9gq.html
http://blog.csdn.net/cocoa_geforce/article/details/6801460

#pragma mark Search Bar Delegate Methods
//  开始
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    isSearching = YES;
    [table reloadData];
}

//  点击搜索
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 
{
    NSString *searchTerm = [searchBar text];
    [self handleSearchForTerm:searchTerm];
}

//  即时搜索
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchTerm
{
    if ([searchTerm length] == 0)
    {
        [self resetSearch];
        [table reloadData];
        return;
    }
    
    [self handleSearchForTerm:searchTerm];
    
}

//  取消搜索
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    isSearching = NO;
    search.text = @"";
    [self resetSearch];
    [table reloadData];
    [searchBar resignFirstResponder];
}




//  提供实时搜索
- (void)handleSearchForTerm:(NSString *)searchTerm
{
    [self resetSearch];
    
    //  总体思路是将不符合条件的加到集合A,然后集中移除集合A
    //  迭代时移除,可能会出错,用过C++的Array就知道
    NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init];
    for (NSString *key in self.keys) 
    {
        NSMutableArray *array = [names valueForKey:key];
        NSMutableArray *toRemove = [[NSMutableArray alloc] init];
        for (NSString *name in array) 
        {
            if ([name rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location == NSNotFound)
                [toRemove addObject:name];
        }
        
        if ([array count] == [toRemove count]) {
            [sectionsToRemove addObject:key];
        }
        
        [array removeObjectsInArray:toRemove];
        [toRemove release];
    }
    
    [self.keys removeObjectsInArray:sectionsToRemove];
    [sectionsToRemove release];
    [table reloadData];
}




//  刷新Keys索引  和  可变副本
- (void)resetSearch {
    
    NSMutableDictionary *allNamesCopy = [self.allNames mutableDeepCopy];
    self.names = allNamesCopy;
    [allNamesCopy release];
	
    NSMutableArray *keyArray = [[NSMutableArray alloc] init];
    [keyArray addObject:UITableViewIndexSearch];//UITableViewIndexSearch=@"{search}" 放大镜,细节啊
    [keyArray addObjectsFromArray:[[self.allNames allKeys] 
        sortedArrayUsingSelector:@selector(compare:)]];// 还进行了排序
    self.keys = keyArray;
    [keyArray release];
}



// NSDictionary 深拷贝
@implementation NSDictionary(DeepMutableCopy)

-(NSMutableDictionary *)mutableDeepCopy
{
	NSMutableDictionary *ret = [[NSMutableDictionary alloc] initWithCapacity:[self count]];
	NSArray *keys = [self allKeys];

	for (id key in keys)
	{
			id oneValue = [self valueForKey:key];
			id oneCopy = nil;
			
			if ([oneValue respondsToSelector:@selector(mutableDeepCopy)])
					oneCopy = [oneValue mutableDeepCopy];
		
			else if ([oneValue respondsToSelector:@selector(mutableCopy)])
					oneCopy = [oneValue mutableCopy];
		
			if (oneCopy == nil)
					oneCopy = [oneValue copy];
		
			[ret setValue:oneCopy forKey:key];
	}
	return ret;
}
@end



#pragma mark Table View Delegate Methods
- (NSIndexPath *)tableView:(UITableView *)tableView 
  willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [search resignFirstResponder];//取消键盘
    search.text = @"";
    isSearching = NO;
    [tableView reloadData];
    return indexPath;
}

- (NSInteger)tableView:(UITableView *)tableView 
sectionForSectionIndexTitle:(NSString *)title 
               atIndex:(NSInteger)index
{
    NSString *key = [keys objectAtIndex:index];
    if (key == UITableViewIndexSearch)
    {
        [tableView setContentOffset:CGPointZero animated:NO];//搜索栏
        return NSNotFound;
    }
    else return index;
    
}

你可能感兴趣的:(search)