//
// ViewController.m
// 联系人管理
//
// Created by 张 文华 on 15/12/15.
// Copyright © 2015年 张 文华. All rights reserved.
//
#import "ViewController.h"
#import "Contact.h"
#import "ShowViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableview;
//@property (weak, nonatomic) IBOutlet UISearchBar *searchbar;
//@property (strong, nonatomic) IBOutlet UISearchDisplayController *searchDisplay;
//@property (strong, nonatomic) IBOutlet UISearchController *searchDisplay;
@end
@implementation ViewController
//模态窗口直接打开就行,不需要函数
- (IBAction)addContact:(UIBarButtonItem *)sender {
//* 模态窗口
// EditViewController *editVC=[EditViewController new];
//
// [self presentViewController:editVC animated:YES completion:nil];
//UIStoryboardSegue* segue = [[UIStoryboardSegue alloc] initWithIdentifier:@"qq" source:self destination:editVC];
//NSLog(@"%@",segue.identifier);
//展示show窗口
//[self performSegueWithIdentifier:@"add" sender:self];
}
#pragma mark 回传数据的代理方法
-(void)backWithContact:(Contact *)contactPsn IsEditing:(BOOL)isEditing
{
if (!isEditing) {
[self.contacts addObject:contactPsn];
}
[self.tableview reloadData];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.contacts=[[NSMutableArray alloc]init];
self.searchResult=[[NSMutableArray alloc]init];
for (int i=0; i<3; i++) {
Contact *contact=[Contact new];
contact.name=[NSString stringWithFormat:@"person%d",i+1];
contact.tel=[NSString stringWithFormat:@"1850036%04d", arc4random_uniform(1000)];
[self.contacts addObject:contact];
}
//设置搜索框
//Pass nil if you wish to display search results in the same view that you are searching.
self.searchDisplay=[[UISearchController alloc]initWithSearchResultsController:nil];
//该对象用来更新数据
self.searchDisplay.searchResultsUpdater=self;
//[self.searchDisplay.searchBar sizeToFit];
//设置是否隐藏导航栏
self.searchDisplay.hidesNavigationBarDuringPresentation=YES;
//self.searchDisplay.searchBar.showsCancelButton=YES;
CGRect frame=self.tableview.tableHeaderView.frame;
frame.size.width=10;
//self.tableview.tableHeaderView.frame=frame;
//设置表格头部即为搜索框
self.tableview.tableHeaderView=self.searchDisplay.searchBar;
//self.searchDisplay.searchBar
//显示取消按钮
[self.searchDisplay.searchBar setShowsCancelButton:YES animated:NO];
self.searchDisplay.dimsBackgroundDuringPresentation=false;
self.tableview.delegate=self;
self.tableview.dataSource=self;
self.searchDisplay.delegate=self;
//self.searchDisplay.delegate=self;
//self.searchbar.delegate=self;
//self.searchDisplay.searchResultsUpdater
// NSLog(@"sss");
}
/*
UISearchController的searchBar中的内容一旦发生变化, 就会调用该方法-updateSearchResultsForSearchController. 在其中, 我们可以使用NSPredicate来设置搜索过滤的条件.
更新UITableView
引入UISearchController之后, UITableView的内容也要做相应地变动: 即cell中要呈现的内容是allCities, 还是filteredCities.
这一点, 可以通过UISearchController的active属性来判断, 即判断输入框是否处于active状态.
UITableView相关的很多方法都要根据active来做判断:
*/
//考虑搜索结果
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (!self.searchDisplay.active) {
return self.contacts.count;
} else {
return self.searchResult.count;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID=@"contactCell";
//1,从重用池中找不用的cell对象
UITableViewCell *cell=[self.tableview dequeueReusableCellWithIdentifier:cellID];
//2,如果没有救自己创建
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
}
Contact *contact=[Contact new];
//3,设置cell的属性
if (!self.searchDisplay.active) {
contact=self.contacts[indexPath.row];
}
else
{
contact=self.searchResult[indexPath.row];
}
//Contact *contact=self.contacts[indexPath.row];
cell.textLabel.text=contact.name;
cell.detailTextLabel.text=contact.tel;
cell.detailTextLabel.textAlignment=NSTextAlignmentLeft;
cell.detailTextLabel.textColor=[UIColor grayColor];
//NSLog(@"%@",contact.tel);
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}
#pragma mark - delegate
#pragma mark 选中单元格时候的动作
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// if (tableView==self.tableview) {
// self.IsSearchResult=NO;
// }
// else
// {
// self.IsSearchResult=YES;
// }
NSLog(@"222");
[self performSegueWithIdentifier:@"show" sender:self];
}
#pragma mark segue跳转前的准备工作
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(@"连线=%@",segue.identifier);
if ([segue.identifier isEqualToString:@"add"]) {
//NSLog(@"****");
EditViewController *editVC=segue.destinationViewController;
editVC.delegate=self;
// NSLog(@"连线");
}
else if([segue.identifier isEqualToString:@"show"])
{
ShowViewController *showVC=segue.destinationViewController;
NSIndexPath *indexpath= [self.tableview indexPathForSelectedRow];
NSLog(@"indexpath.row=%ld",indexpath.row);
if (self.searchDisplay.active)
{
showVC.contactPsn=self.searchResult[indexpath.row];
}
else
{
showVC.contactPsn=self.contacts[indexpath.row];
}
}
}
//#pragma mark - searchBar代理方法
//-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
//{
// // NSLog(@"%@",self.searchbar.text);
//}
//
/*
下面的方法并没有执行
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
NSLog(@"searchBarTextDidEndEditing");
self.IsSearchResult=YES;
[self.searchDisplay.searchBar resignFirstResponder];
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
NSLog(@"searchBarTextDidBeginEditing");
self.IsSearchResult=YES;
[self.searchDisplay.searchBar becomeFirstResponder];
}
//-(void)didPresentSearchController:(UISearchController *)searchController
//{
//
//}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
NSLog(@"searchBarSearchButtonClicked");
self.IsSearchResult=YES;
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
NSLog(@"searchBarCancelButtonClicked");
self.IsSearchResult=NO;
}
*/
#pragma mark - UISearchResultsUpdating 代理协议
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
//self.searchDisplay.searchBar
[self.searchResult removeAllObjects];
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"SELF.name CONTAINS[c] %@", self.searchDisplay.searchBar.text];
self.searchResult = [[self.contacts filteredArrayUsingPredicate:searchPredicate] mutableCopy];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableview reloadData];
});
}
/*UISearchController的移除
在viewWillDisappear中要将UISearchController移除, 否则切换到下一个View中, 搜索框仍然会有短暂的存在.*/
//不确定该方法是否有用
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (self.searchDisplay.active) {
self.searchDisplay.active = NO;
[self.searchDisplay.searchBar removeFromSuperview];
}
}
@end