简单的TableView
2014-07-24 18:29
316 查看
背景知识
每个表都是UITableView的实例,表中的每一行都是UITableViewCell的实例。TableView的种类
Grouped table
Plain table without index
Plain table with index
// // ViewController.m // Contact // // Created by Norcy on 15/4/16. // Copyright (c) 2015年 Norcy. All rights reserved. // #import "ViewController.h" @interface ViewController () { } @property (strong, nonatomic) NSDictionary* names; @property (strong, nonatomic) NSArray* keys; @end NSMutableArray* filteredNames; UISearchDisplayController* searchController; static NSString* CELL_ID = @"MyCell"; @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //Screen int screenWidth = [[UIScreen mainScreen] bounds].size.width; int screenHeight = [[UIScreen mainScreen] bounds].size.height; //Filter filteredNames = [[NSMutableArray alloc] init]; //TableView UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)]; [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CELL_ID]; tableView.delegate = self; tableView.dataSource = self; tableView.tag = 1; UIEdgeInsets insets = tableView.contentInset; //设置与屏幕顶部的距离,防止被状态栏挡住 insets.top = 20; [tableView setContentInset:insets]; //Search Bar UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight / 12)]; tableView.tableHeaderView = searchBar; searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; searchController.delegate = self; searchController.searchResultsDataSource = self; //Names and Keys NSString* path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"]; self.names = [NSDictionary dictionaryWithContentsOfFile:path]; self.keys = [[self.names allKeys] sortedArrayUsingSelector:@selector(compare:)]; [self.view addSubview:tableView]; } #pragma mark - #pragma mark Data Source Methods - (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView { if (tableView.tag == 1) return [self.keys count]; else return 1; } - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section { if (tableView.tag == 1) { NSString* key = self.keys[section]; NSArray* curSection = self.names[key]; return [curSection count]; } else return [filteredNames count]; } - (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section { if (tableView.tag == 1) return self.keys[section]; else return nil; } - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CELL_ID forIndexPath:indexPath]; // UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CELL_ID]; // if (cell == nil) // { // NSLog(@"asd"); // cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELL_ID]; // } if (tableView.tag == 1) { NSString* key = self.keys[indexPath.section]; NSArray* curSection = self.names[key]; cell.textLabel.text = curSection[indexPath.row]; } else cell.textLabel.text = filteredNames[indexPath.row]; return cell; } #pragma mark Delegate Methods //显示索引 - (NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView { if (tableView.tag == 1) return self.keys; else return nil; } - (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; } #pragma mark Search Delegate Methods - (void)searchDisplayController:(UISearchDisplayController*)controller didLoadSearchResultsTableView:(UITableView*)tableView { [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CELL_ID]; } - (BOOL)searchDisplayController:(UISearchDisplayController*)controller shouldReloadTableForSearchString:(NSString*)searchString { [filteredNames removeAllObjects]; if (searchString.length > 0) { NSPredicate* predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary* bindings) { NSRange range = [evaluatedObject rangeOfString:searchString options:NSCaseInsensitiveSearch]; return range.location != NSNotFound; }]; for (NSString* key in self.keys) { NSArray* matches = [self.names[key] filteredArrayUsingPredicate:predicate]; [filteredNames addObjectsFromArray:matches]; } } return YES; } @end
View Code
相关文章推荐
- iOS学习之Table View的简单使用
- Qt TableView的简单使用
- iOS学习之Table View的简单使用
- iOS学习之Table View的简单使用
- tableView下拉刷新,上拉加载简单应用
- Iphone开发(十)简单的列表tableView与行的响应事件
- 2.关于QT中数据库操作,简单数据库连接操作,数据库的增删改查,QSqlTableModel和QTableView,事务操作,关于QItemDelegate 代理
- iOS学习之Table View的简单使用
- iOS学习之Table View的简单使用
- Qt TableView的简单使用
- 一些简单样式控制(包括GirdView、table)
- iOS学习之Table View的简单使用
- 关于tableView的简单实例
- IOS Xcode5 IB tableView 简单使用
- iOS tableview 实现简单的搜索功能
- ios开发入门- tableview 简单使用(一)
- iOS学习笔记2—关于tableView的一些简单操作
- 简单tableView的使用
- 自定义简单的TableView
- iOS tableview 实现简单的搜索功能