//
// ViewController.m
// Project New
//
// Created by sjq-4-1 on 16/10/8.
// Copyright © 2016年 sjq-4-1. All rights reserved.
//
//h.文件
@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchResultsUpdating>
#import "ViewController.h"
#import "ToDoItem.h"
#import "AddItemViewController.h"
#import "CustomTableViewCell.h"
@interface ViewController ()
@property (strong,nonatomic) NSMutableArray *toDoList;
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (nonatomic,strong) UISearchController *searchController;
@property (strong,nonatomic) NSMutableArray *searchResult;
//建立索引数组
@property (strong,nonatomic) NSMutableArray *dataSource,*dataBase;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchController.searchResultsUpdater = self;
_searchController.dimsBackgroundDuringPresentation = NO;
_searchController.hidesNavigationBarDuringPresentation = NO;
_searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
self.tableview.tableHeaderView = self.searchController.searchBar;
[self initialList];
}
- (void)initialList {
self.toDoList = [NSMutableArray array];
ToDoItem *tempItem = nil;
for (int i='A'; i<='Z'; i++) {
tempItem = [ToDoItem new];
NSString *tempName = [NSString stringWithFormat:@"%c 项目",i];
tempItem.itemName = tempName;
[self.toDoList addObject:tempItem];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//接受来自添加页面获得 的新信息
-(IBAction)unwindToList:(UIStoryboardSegue *)segue {
AddItemViewController *addVC = segue.sourceViewController;
ToDoItem *item = addVC.toDoItem;
if (item != nil) {
[self.toDoList addObject:item];
}
//刷新页面
[self.tableview reloadData];
}
//两个协议的必须实现的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (self.searchController.active) {
return self.searchResult.count;
}else{
return self.toDoList.count;
}
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *reuseIdentifier= @"reuseIdentifier";
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
cell = [[CustomTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
ToDoItem *item =nil;
if (self.searchController.active) {
item = [self.searchResult objectAtIndex:indexPath.row];
}else{
item = [self.toDoList objectAtIndex:indexPath.row];
}
// cell.textLabel.text = item.itemName;
UIImage *image1 = [UIImage imageNamed:@"zj.jpg"];
CGSize size = cell.UIImageview.frame.size;
cell.UIImageview.image = [self OriginImage:image1 scaleToSize:size];
// cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.UIName.text = item.itemName;
cell.UIStateTime.text = @"0.5 小时";
return cell;
}
-(UIImage *)OriginImage:(UIImage *)image scaleToSize:(CGSize)size{
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
//搜索需实现函数
- (void) updateSearchResultsForSearchController:(UISearchController *)searchController{
NSString *searchString = [self.searchController.searchBar text];
NSPredicate *preicate = [NSPredicate predicateWithFormat:@"itemName contains[c] %@",searchString];
if (self.searchResult!=nil) {
[self.searchResult removeAllObjects];
}
// 过滤数据
self.searchResult = [NSMutableArray arrayWithArray:[_toDoList filteredArrayUsingPredicate:preicate]];
//刷新数据
[self.tableview reloadData];
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
@end