`
fireflylover
  • 浏览: 107949 次
  • 性别: Icon_minigender_2
  • 来自: 武汉
社区版块
存档分类
最新评论

IPhone 之 UITableView 带搜索

阅读更多

@interface TableViewAppDelegate : NSObject <UIApplicationDelegate> {
    
    UIWindow *window;
    UINavigationController *navigationController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@end


 
#import "TableViewAppDelegate.h"
#import "RootViewController.h"


@implementation TableViewAppDelegate

@synthesize window;
@synthesize navigationController;


#pragma mark -
#pragma mark Application lifecycle

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    
    // Override point for customization after app launch    
 
[window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
}


- (void)applicationWillTerminate:(UIApplication *)application {
// Save data if appropriate
}


#pragma mark -
#pragma mark Memory management

- (void)dealloc {
[navigationController release];
[window release];
[super dealloc];
}


@end






@interface RootViewController : UITableViewController 
<UISearchBarDelegate>{ 
    NSDictionary *movieTitles; 
    NSArray *years; 
    
    //---search--- 
    IBOutlet UISearchBar *searchBar; 
    BOOL isSearchOn; 
    BOOL canSelectRow;    
    NSMutableArray *listOfMovies; 
    NSMutableArray *searchResult; 
} 
@property (nonatomic, retain) NSDictionary *movieTitles; 
@property (nonatomic, retain) NSArray *years; 
//---search--- 
@property (nonatomic, retain) UISearchBar *searchBar; 
- (void) doneSearching: (id)sender; 
- (void) searchMoviesTableView; 
@end 





 
#import "RootViewController.h"


@implementation RootViewController
@synthesize movieTitles, years; 
@synthesize searchBar; 
- (void)viewDidLoad { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Movies" 
ofType:@"plist"]; 
    NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:path]; 
    self.movieTitles = dic; 
    
    NSArray *array = [[movieTitles allKeys] 
  sortedArrayUsingSelector:@selector(compare:)]; 
    self.years = array;    
    [dic release]; 
    //---display the searchbar--- 
    self.tableView.tableHeaderView = searchBar; 
    searchBar.autocorrectionType = UITextAutocorrectionTypeYes; 
    //---copy all the movie titles in the dictionary into the listOfMovies array--- 
    listOfMovies = [[NSMutableArray alloc] init]; 
 
    for (NSString *year in array)    //---get all the years--- 
    { 
        //---get all the movies for a particular year--- 
        NSArray *movies = [movieTitles objectForKey:year];   
        for (NSString *title in movies)   
        { 
            [listOfMovies addObject:title]; 
        } 
    } 
    //---used for storing the search result--- 
    searchResult = [[NSMutableArray alloc] init]; 
    
    isSearchOn = NO; 
    canSelectRow = YES; 
    [super viewDidLoad]; 
} 






//---fi red when the user taps on the searchbar---   光标聚焦
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { 
    isSearchOn = YES; 
    canSelectRow = NO; 
    self.tableView.scrollEnabled = NO; 
    //---add the Done button at the top--- 
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]              
   
  initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
  target:self action:@selector(doneSearching:)] autorelease]; 
} 

//---done with the searching---  点击done按钮
- (void) doneSearching:(id)sender { 
    isSearchOn = NO; 
    canSelectRow = YES; 
    self.tableView.scrollEnabled = YES; 
    self.navigationItem.rightBarButtonItem = nil; 
    //---hides the keyboard--- 
    [searchBar resignFirstResponder]; 
    //---refresh the TableView--- 
    [self.tableView reloadData]; 
} 


//---fi red when the user types something into the searchbar---  有值输入
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { 
    
//---if there is something to search for---  改变cell值 和 去掉tableView section(通过预先设定好的标志)  
    if ([searchText length] > 0) { 
        isSearchOn = YES; 
        canSelectRow = YES; 
        self.tableView.scrollEnabled = YES; 
        [self searchMoviesTableView]; 
    } 
    else {        
        //---nothing to search--- 
        isSearchOn = NO; 
        canSelectRow = NO; 
        self.tableView.scrollEnabled = NO; 
    }    
    [self.tableView reloadData]; 
} 

- (void) searchMoviesTableView { 
    //---clears the search result--- 
    [searchResult removeAllObjects]; 
    //过滤搜索结果
    for (NSString *str in listOfMovies) 
    { 
//指定开始 匹配的location 和最大匹配长度
//NSRange r;
// r.location = 0;
// r.length = 2;
        NSRange titleResultsRange = [str rangeOfString:searchBar.text 
  options:NSCaseInsensitiveSearch range:r];
NSLog(@"%@  %d   %d",str,titleResultsRange.length,titleResultsRange.location);
        if (titleResultsRange.length > 0) 
            [searchResult addObject:str]; 
    } 
NSLog(@"%d",[searchResult count]);
} 

///---fi red when the user taps the Search button on the keyboard--- 
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
[self searchMoviesTableView]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    if (isSearchOn) 
       return 1; 
    else 
        return [years count];    
} 

// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section { 
    if (isSearchOn) { 
        return [searchResult count]; 
    } else 
    { 
        NSString *year = [years objectAtIndex:section]; 
        NSArray *movieSection = [movieTitles objectForKey:year]; 
       return [movieSection count]; 
  } 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    
    static NSString *CellIdentifier = @"Cell"; 
    
    UITableViewCell *cell = [tableView 
dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
  reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    
    // Configure the cell. 
    if (isSearchOn) {        
        NSString *cellValue = [searchResult objectAtIndex:indexPath.row]; 
        cell.textLabel.text = cellValue;        
    } else { 
        NSString *year = [years objectAtIndex:[indexPath section]]; 
        NSArray *movieSection = [movieTitles objectForKey:year]; 
        cell.textLabel.text = [movieSection objectAtIndex:[indexPath row]]; 
    } 
    return cell; 
} 

// 分区标题
- (NSString *)tableView:(UITableView *)tableView 
titleForHeaderInSection:(NSInteger)section { 
    NSString *year = [years objectAtIndex:section]; 
    if (isSearchOn) 
        return nil; 
    else 
        return year; 
}

// tableView 右侧索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { 
    if (isSearchOn) 
        return nil; 
    else 
        return years; 
} 

//---fired before a row is selected---  cell是否可选
- (NSIndexPath *)tableView :(UITableView *)theTableView 
   willSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (canSelectRow) 
        return indexPath; 
    else 
        return nil; 
} 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 
    
    // Release any cached data, images, etc that aren't in use. 
} 
- (void)viewDidUnload { 
    // Release anything that can be recreated in viewDidLoad or on demand. 
    // e.g. self.myOutlet = nil; 
} 
- (void)dealloc { 
    [years release]; 
    [movieTitles release]; 
    [searchBar release]; 
    [super dealloc]; 
} 
@end
 
分享到:
评论

相关推荐

    iphone 关于UITableView的排序,搜索、使用Interface Builder创建等

    iphone 关于UITableView的排序,搜索、使用Interface Builder创建等iphone 关于UITableView的排序,搜索、使用Interface Builder创建等

    UITableView删除功能(非编辑模式)

    这几天在和一个搞Iphone编程的一起探讨一个关于TableViewCell的删除问题,在NAvigation里面添加Cell然后删除,不要Iphone开发基础教程中的呢样,在每一个cell中添加一个按钮,点击按钮直接删除该行,在CC上搜索很多...

    iPhone开发秘籍

    大量未公开的绝技,带你深入iPhone开发秘境 任务驱动,丰富的实战代码,让你触类旁通 目录 第1章 iphone sdk简介. 1 1.1 苹果公司的iphone sdk 1 1.2 组建iphone项目 2 1.3 iphone应用程序组件 3 1.3.1 应用...

    iPhone开发秘籍.part2.rar

    iPhone 和iPod touch 随带的许多乃至大 部分应用程序都以表格为中心,包括Settings、YouTube、Stocks 和Weather。第5章展示iPhone 表 格的工作方式,哪些表格对于开发人员可用,以及如何在自己的程序中使用表格特性...

    iPhone开发秘籍.part4.rar

    iPhone 和iPod touch 随带的许多乃至大 部分应用程序都以表格为中心,包括Settings、YouTube、Stocks 和Weather。第5章展示iPhone 表 格的工作方式,哪些表格对于开发人员可用,以及如何在自己的程序中使用表格特性...

    iPhone开发秘籍.part1.rar

    iPhone 和iPod touch 随带的许多乃至大 部分应用程序都以表格为中心,包括Settings、YouTube、Stocks 和Weather。第5章展示iPhone 表 格的工作方式,哪些表格对于开发人员可用,以及如何在自己的程序中使用表格特性...

    iphone通讯录的简单实现

    用UITableView实现我的通讯录的功能,包括索引功能、名字的头字母的排序等,搜索功能还没实现。

    IOS编程入门-精品教程

    第一部分:Hello World!...第十三部分:在表视图中添加搜索栏 第十四部分:如何在导航界面隐藏Tab Bar 第十五部分:Objective-C基础知识-介绍面向对象的编程 第十六部分:如何在你的iPhone App中发送邮件

    一步步学IOS5编程完整版

    第十三部分:在表视图中添加搜索栏 第十四部分:如何在导航界面隐藏Tab Bar 第十五部分:Objective-C基础知识-介绍面向对象的编程 第十六部分:如何在你的iPhone App中发送邮件 第十七部分:持续改善Recipe App的...

    一步一步学习 iOS 6 编程(第四版)

    第十三部分:在表视图中添加搜索栉. 第十四部分:如何在导航界面隐藏 Tab Bar 第十五部分:Objective-C 基础知识-介绍面向对象的编程 第十六部分:如何在你的 iPhone App 中发送邮件 第十七部分:持续改善 Recipe App 的...

    史上最全的ios开发源码

    列表-UITableView背景随动 列表类》》自定义Table View折叠效果 列表类-FormInputAccessoryView 列表类-Grid TableView 列表类-Grouped TableView With Shadows 列表类--iOS 6.0 Pull to Refresh 列表类--Section...

    一步一步学习IOS6

    第十三部分:在表视图中添加搜索栏 第十四部分:如何在导航界面隐藏Tab Bar 第十五部分:Objective-C基础知识-介绍面向对象的编程 第十六部分:如何在你的iPhone App中发送邮件 第十七部分:持续改善Recipe App的...

    一步一步学习_iOS_6_编程(第四版)

    第十三部分:在表视图中添加搜索栏 第十四部分:如何在导航界面隐藏Tab Bar 第十五部分:Objective-C基础知识-介绍面向对象的编程 第十六部分:如何在你的iPhone App中发送邮件 第十七部分:持续改善Recipe App的...

    CustomSearch:UISearchDisplayController适配

    CustomSearch UISearchDisplayController适配 ...本Demo的搜索栏UISearchBar脱离UITableView的tableHeaderView而存在,做了对iOS7,8的适配 ##效果图 ##测试环境 Xcode 6.4,模拟器iPhone5,5s,6,6 plus,真机5 iOS8.1

Global site tag (gtag.js) - Google Analytics