iOS开发学习之#简单通讯录的制作#
2014-12-03 10:59
459 查看
(1)创建一个项目telephoneBook
(2)打开Main.storyboard文件,从视图库图拖一个Tab bar Controller标签栏控制器到画布中。
(3)在工具栏中,选择show the Attributes inspector图标,在View controller下,选择Is Initial View Controller。
(4)将画布中的原有的view controller视图控制器删掉,再将Tab Bar controller标签栏控制器关联view controller-Item 1和view controller-Item2视图控制器的视图删除。
(5)从视图库中拖一个Navigation controller导航控制器到画布中。
(6)将Tab Bar Controller标签栏控制器关联视图变为Navigation Controller导航控制器。
核心代码:
TableView1.h
TableView1.m
#import "TableView1.h"
@interface TableView1 ()
@end
@implementation TableView1
- (void)viewDidLoad {
a = [[NSMutableArray alloc]initWithObjects:@"ant",@"alpaca",@"albatross",@"badger",@"bat",@"bear",@"cat",@"calf",@"cattle", nil];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [a 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];
}
cell.textLabel.text = [a objectAtIndex:[indexPath row]];
return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[a removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
- (IBAction)aa:(id)sender {
[self setEditing:YES];
}
- (IBAction)bb:(id)sender {
[self setEditing:NO];
}
@end
TableView2.h
#import <UIKit/UIKit.h>
@interface TableView2 : UITableViewController<UITableViewDataSource,UITableViewDelegate>{
NSDictionary *list;
NSArray *ff;
IBOutlet UISearchBar *searchBar;
BOOL isSearchOn;
BOOL canSelectRow;
NSMutableArray *listOfMovies;
NSMutableArray *searchResult;
}
- (void)searchMoviesTableView;
@end
TableView2.m
(2)打开Main.storyboard文件,从视图库图拖一个Tab bar Controller标签栏控制器到画布中。
(3)在工具栏中,选择show the Attributes inspector图标,在View controller下,选择Is Initial View Controller。
(4)将画布中的原有的view controller视图控制器删掉,再将Tab Bar controller标签栏控制器关联view controller-Item 1和view controller-Item2视图控制器的视图删除。
(5)从视图库中拖一个Navigation controller导航控制器到画布中。
(6)将Tab Bar Controller标签栏控制器关联视图变为Navigation Controller导航控制器。
核心代码:
TableView1.h
#import <UIKit/UIKit.h> @interface TableView1 : UITableViewController<UITableViewDataSource,UITableViewDelegate>{ NSMutableArray *a; } - (IBAction)aa:(id)sender; - (IBAction)bb:(id)sender; @end
TableView1.m
#import "TableView1.h"
@interface TableView1 ()
@end
@implementation TableView1
- (void)viewDidLoad {
a = [[NSMutableArray alloc]initWithObjects:@"ant",@"alpaca",@"albatross",@"badger",@"bat",@"bear",@"cat",@"calf",@"cattle", nil];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [a 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];
}
cell.textLabel.text = [a objectAtIndex:[indexPath row]];
return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[a removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
- (IBAction)aa:(id)sender {
[self setEditing:YES];
}
- (IBAction)bb:(id)sender {
[self setEditing:NO];
}
@end
TableView2.h
#import <UIKit/UIKit.h>
@interface TableView2 : UITableViewController<UITableViewDataSource,UITableViewDelegate>{
NSDictionary *list;
NSArray *ff;
IBOutlet UISearchBar *searchBar;
BOOL isSearchOn;
BOOL canSelectRow;
NSMutableArray *listOfMovies;
NSMutableArray *searchResult;
}
- (void)searchMoviesTableView;
@end
TableView2.m
#import "TableView2.h" @interface TableView2 () @end @implementation TableView2 - (void)viewDidLoad { NSString *path = [[NSBundle mainBundle]pathForResource:@"1" ofType:@"plist"]; NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:path]; list = dic; NSArray *array = [[list allKeys]sortedArrayUsingSelector:@selector(compare:)]; ff = array; self.tableView.tableHeaderView = searchBar; searchBar.autocorrectionType = UITextAutocorrectionTypeYes; listOfMovies = [[NSMutableArray alloc]init]; for (NSString *year in array) { NSArray *movies = [list objectForKey:year]; for (NSString *title in movies) { [listOfMovies addObject:title]; } } searchResult = [[NSMutableArray alloc]init]; isSearchOn = NO; canSelectRow = YES; [super viewDidLoad]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source - (NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if (canSelectRow) { return indexPath; }else{ return nil; } } - (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)searchBarSearchButtonClicked:(UISearchBar*)searchBar{ [self searchMoviesTableView]; } - (void) searchMoviesTableView{ [searchResult removeAllObjects]; for (NSString *str in listOfMovies) { NSRange titleResultRange = [str rangeOfString:searchBar.text options:NSCaseInsensitiveSearch]; if (titleResultRange.length > 0) { [searchResult addObject:str]; } } } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ if (isSearchOn) { return 1; }else{ return [ff count]; } } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (isSearchOn) { return [searchResult count]; }else{ NSString *year = [ff objectAtIndex:section]; NSArray *movieSection = [list 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]; } if (isSearchOn) { NSString *cellValue = [searchResult objectAtIndex:indexPath.row]; cell.textLabel.text = cellValue; }else{ NSString *year = [ff objectAtIndex:[indexPath section]]; NSArray *movieSection = [list objectForKey:year]; cell.textLabel.text = [movieSection objectAtIndex:[indexPath row]]; } return cell; } - (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ NSString *year = [ff objectAtIndex:section]; if (isSearchOn) { [searchBar resignFirstResponder]; return nil; }else{ return year; } } @end
相关文章推荐
- IOS开发-UI学习-UIWebView,简单浏览器的制作
- 【iOS-Cocos2d游戏开发】使用plist文件制作简单精灵
- 黑马程序员---ios开发---objective-c学习-01-简单类实现常见错误及分析
- iOS开发 UIDatePicker简单的日历的制作
- iOS开发RunLoop学习:一:RunLoop简单介绍
- iOS-Cocos2d游戏开发】使用plist文件制作简单精灵
- iOS开发脚踏实地学习day12-通讯录和plist存储
- IOS开发-UI学习-使用UIImageView控件制作动画
- 【学习中的分享】 iOS利用UILabel和UIButton制作一个简单的计算器
- iOS开发-------简单通讯录(UITableView和CoreData的应用)
- iOS开发学习001_01:Address Book 01 显示通讯录中某人姓名、电话
- 黑马程序员——iOS 开发学习---控件制作登录注册页面
- 【iOS-Cocos2d游戏开发之二十】精灵的基础知识点总汇(位图操作/贴图更换/重排z轴等)及制作简单遮盖层!
- 【iOS开发-114】ABAddressBook通讯录:增删改查的简单介绍,CF框架于F框架间的转换
- Android 多媒体开发学习之制作一个简单的画板
- iOS 9学习系列:UIStackView如何让你的开发更简单
- iOS开发学习-类似微信聊天消息中的电话号码点击保存到通讯录中的功能
- salesforce 零基础开发入门学习(八)数据分页简单制作
- 动态网站开发技术学习2:VS 2010制作作第一个简单示例网站
- iOS 开发学习39 简单学习反射的部分用法