表视图的范例
来源:互联网 发布:visual mac 编辑:程序博客网 时间:2024/05/17 08:27
[转载地址]:http://blog.csdn.net/riveram/article/details/7340479
1.创建一个Navigation—based—Application项目,这样Interface Builder中会自动生成一个Table View,然后将Search Bar拖放到表示图上,以我们要给表示图添加搜索功能,不要忘记将Search Bar的delegate连接到File‘s Owner项,然后将Search Bar与searchBar变量连接。
2.在Resources文件夹下创建一个Movies.plist文件,然后为该文件添加一些数据,如下图:
3.在.h头文件添加如下内容:
- #import <UIKit/UIKit.h>
- @interface MyTableView : UITableViewController <UISearchBarDelegate>{
- NSDictionary *movieTitles;
- NSArray *years;
- IBOutlet UISearchBar *searchBar;
- BOOL isSearchOn;
- BOOL canSelectRow;
- //下面两个是搜索用到的两个变量
- NSMutableArray *listOfMovies;
- NSMutableArray *searchResult;
- }
- @property(nonatomic,retain) NSDictionary *movieTitles;
- @property(nonatomic,retain)NSArray *years;
- @property(nonatomic,retain)UISearchBar *searchBar;
- -(void)donSearching:(id)sender;
- -(void)searchMoviesTableView;
- @end
#import <UIKit/UIKit.h>@interface MyTableView : UITableViewController <UISearchBarDelegate>{ NSDictionary *movieTitles; NSArray *years; IBOutlet UISearchBar *searchBar; BOOL isSearchOn; BOOL canSelectRow; //下面两个是搜索用到的两个变量 NSMutableArray *listOfMovies; NSMutableArray *searchResult;}@property(nonatomic,retain) NSDictionary *movieTitles;@property(nonatomic,retain)NSArray *years;@property(nonatomic,retain)UISearchBar *searchBar;-(void)donSearching:(id)sender;-(void)searchMoviesTableView;@end
4.当加载View窗口时,首先定位属性列表并把这个列表加载到listOfMovies中,然后将所有的年份提取到years中,然后添加搜索条并初始化搜索条用到的数据:
- //读取Movies.plist文件的内容到变量里面
- - (void)viewDidLoad
- {
- NSString *path = [[NSBundle mainBundle]pathForResource:@"Movies" ofType:@"plist"];
- NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:path];
- self.movieTitles = dic;
- [dic release];
- NSArray *array = [[self.movieTitles allKeys]sortedArrayUsingSelector:@selector(compare:)];
- self.years = array;
- //下面两句是添加搜索条
- self.tableView.tableHeaderView = searchBar;
- self.searchBar.autocorrectionType = UITextAutocorrectionTypeYes;
- //初始化listofmovies
- listOfMovies = [[NSMutableArray alloc]init];
- for (NSString *year in years) {
- NSArray *movies = [movieTitles objectForKey:year];
- for(NSString *title in movies){
- [listOfMovies addObject:title];
- }
- }
- searchResult = [[NSMutableArray alloc]init];
- isSearchOn = NO;
- canSelectRow = YES;
- [super viewDidLoad];
- }
//读取Movies.plist文件的内容到变量里面- (void)viewDidLoad{ NSString *path = [[NSBundle mainBundle]pathForResource:@"Movies" ofType:@"plist"]; NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:path]; self.movieTitles = dic; [dic release]; NSArray *array = [[self.movieTitles allKeys]sortedArrayUsingSelector:@selector(compare:)]; self.years = array; //下面两句是添加搜索条 self.tableView.tableHeaderView = searchBar; self.searchBar.autocorrectionType = UITextAutocorrectionTypeYes; //初始化listofmovies listOfMovies = [[NSMutableArray alloc]init]; for (NSString *year in years) { NSArray *movies = [movieTitles objectForKey:year]; for(NSString *title in movies){ [listOfMovies addObject:title]; } } searchResult = [[NSMutableArray alloc]init]; isSearchOn = NO; canSelectRow = YES; [super viewDidLoad];}
5.在自动生成的方法numberOfSectionsInTableView中添加如下代码,表示告诉表示图一共分多少节:
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- if (isSearchOn) {
- return 1;//如果正在搜索就只有一个section
- }
- else
- return [self.years count];
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- if (isSearchOn) {
- return [searchResult count];
- }else{
- // Return the number of rows in the section.
- NSString *year = [self.years objectAtIndex:section];
- NSArray *movieSection = [self.movieTitles objectForKey:year];
- return [movieSection count];
- }
- }
- - (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];
- }
- if (isSearchOn) {
- NSString *cellValue = [searchResult objectAtIndex:indexPath.row];
- cell.textLabel.text = cellValue;
- }else{
- NSString *year = [self.years objectAtIndex:[indexPath section]];//得到当前行所在的section
- NSArray *movieSection = [self.movieTitles objectForKey:year];
- cell.textLabel.text = [movieSection objectAtIndex:[indexPath row]];
- cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
- }
- //为每一行添加图片
- UIImage *image = [UIImage imageNamed:@"apple.jpeg"];
- cell.imageView.image = image;
- return cell;
- }
- //设置每个section的标题
- -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
- NSString *year = [self.years objectAtIndex:section];
- if (isSearchOn) {
- return nil;
- }
- else{
- return year;
- }
- }
- //添加索引
- -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
- if (isSearchOn)
- return nil;
- else
- return years;
- }
- //搜索筐得到焦点后
- -(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
- isSearchOn = YES;
- canSelectRow = NO;
- self.tableView.scrollEnabled = NO;
- //添加down按钮及其点击方法
- self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donSearching:)]autorelease];
- }
- //点击down按钮后
- -(void)donSearching:(id)sender{
- isSearchOn = NO;
- canSelectRow = YES;
- self.tableView.scrollEnabled = YES;
- self.navigationItem.rightBarButtonItem = nil;
- [searchBar resignFirstResponder];
- [self.tableView reloadData];
- }
- //搜索筐里面的文字改变后
- -(void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
- if ([searchText length]>0) {
- isSearchOn = YES;
- canSelectRow = YES;
- self.tableView.scrollEnabled = YES;
- [self searchMoviesTableView];//调用搜索方法
- }
- else{
- isSearchOn = NO;
- canSelectRow = NO;
- self.tableView.scrollEnabled = NO;
- }
- [self.tableView reloadData];
- }
- //自定义的搜索方法,得到搜索结果
- -(void)searchMoviesTableView{
- [searchResult removeAllObjects];
- for (NSString *str in listOfMovies) {
- NSRange titleResultsRange = [str rangeOfString:searchBar.text options:NSCaseInsensitiveSearch];
- if (titleResultsRange.length > 0) {
- [searchResult addObject:str];
- }
- }
- }
- -(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar{
- [self searchMoviesTableView];
- }
- //点击table某一行跳转页面
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- MyTableViewOneMessage *mytm = [[MyTableViewOneMessage alloc]initWithNibName:@"MyTableViewOneMessage" bundle:nil];
- NSString *year = [self.years objectAtIndex:[indexPath section]];
- NSArray *movieSection = [self.movieTitles objectForKey:year];
- NSString *movieTitle = [movieSection objectAtIndex:[indexPath row]];
- NSString *message = [[NSString alloc]initWithFormat:@"%@",movieTitle];
- mytm.message = message;
- [self.navigationController pushViewController:mytm animated:YES];
- [mytm release];
- }
Interface Builder中添加两个lable,具体的就不详细了,很简单的,下面是这个界面的.h和.m文件:
- #import <UIKit/UIKit.h>
- @interface MyTableViewOneMessage : UIViewController {
- IBOutlet UILabel *mylable;
- NSString *message;
- }
- @property(nonatomic,retain)UILabel *mylable;
- @property(nonatomic,retain)NSString *message;
- @end
- #import "MyTableViewOneMessage.h"
- @implementation MyTableViewOneMessage
- @synthesize mylable;
- @synthesize message;
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- -(void)viewDidAppear:(BOOL)animated{
- self.mylable.text = message;
- }
- - (void)dealloc
- {
- [mylable release];
- [message release];
- [super dealloc];
- }
- - (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.
- }
- #pragma mark - View lifecycle
- - (void)viewDidLoad
- {
- self.navigationItem.title = @"Tableview传过来的值";
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- }
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- // Return YES for supported orientations
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- @end
- 表视图的范例
- 第十二章:表视图常用UI范例——保存和恢复自定义的UITableViewCell中UITextField中输入的值
- 第十二章:表视图常用UI范例——下拉刷新+无限滚动
- [译]Android 泄露范例: 视图订阅
- 主从表的报表(简单范例)
- sqlserver一个动态交叉表的范例
- 1 范例数据库表的建立
- TP的多表查询+分页范例
- 关于kudu操作表的范例
- 表、视图、实体化视图的比较
- 第十二章:表视图常用UI范例——实现折叠列表,以及cell增加滑动手势
- XML+CSS的范例
- LOG4J的配置范例
- Kernel Dll的范例
- LOG4J的配置范例
- BindingSource的使用范例
- 简单的AJAX范例
- GLSL的简单范例
- maven创建java工程及web工程
- 使用DbVisualizer划线执行SQL语句的时候提示Selection executes are onlyallowed in DbVisualizer Personel edition
- 输出1000以内所有素数
- 大奖赛计分
- JPEG文件格式
- 表视图的范例
- C++primer习题编写Sales_item类
- python utc gmt 时间 格式转换
- Fedora 17 安装Devstack后打开界面403拒绝访问错误
- Unable to convert string form to class javax.el.ValueExpression的一种解决方案
- C++第十一周任务-等差数列
- 体验三元运算O(∩_∩)O哈!
- linux__framebuffer__认识
- 1-1000的回文数