实例介绍
【实例截图】
【核心代码】
// // CustomTableViewController.m // Memorandum // // Created by Empty Brain on 15/8/9. // Copyright (c) 2015年 www.lzqok.cn. All rights reserved. // #import "CustomTableViewController.h" #import "CustomCell.h" #import "EditViewController.h" @interface CustomTableViewController () { NSMutableArray *dataList; NSMutableArray *searDataList; } @end @implementation CustomTableViewController @synthesize db; - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //self.navigationController.navigationBar.translucent = NO; //self.tabBarController.tabBar.translucent=NO; // UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 35)]; searDataList = [[NSMutableArray alloc]init]; _searchBar = [[UISearchBar alloc]init]; _searchBar.delegate = self; self.searchBar.showsScopeBar = YES; _searchBar.frame = CGRectMake(0, 0, 200, 35); self.tableView.tableHeaderView = _searchBar; // [self.searchBar setBarTintColor:[UIColor redColor]]; // [_searchBar setBackgroundColor:[UIColor clearColor]]; /* _searchBar.layer.cornerRadius = 18; _searchBar.layer.masksToBounds = YES; [_searchBar.layer setBorderColor:[UIColor whiteColor].CGColor]; [_searchBar.layer setBorderWidth:8]; */ // [titleView addSubview:searchBar]; [self.navigationItem.titleView sizeToFit]; //self.navigationItem.titleView = titleView; _searchDisCtrl = [[UISearchDisplayController alloc]initWithSearchBar:_searchBar contentsController:self]; _searchDisCtrl.delegate = self; _searchDisCtrl.searchResultsDataSource = self; _searchDisCtrl.searchResultsDelegate = self; db = [[DBUtils alloc]init]; // self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0); self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [self setExtraCellLineHidden:self.tableView]; [self setbeginRefreshing]; UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil]; self.navigationItem.title = @"备忘录"; [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:[UIColor blueColor]}]; self.navigationItem.backBarButtonItem = item; } - (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; // [self.tableView setEditing:YES animated:YES]; } - (void)viewWillAppear:(BOOL)animated{ [self updateData]; [_searchBar resignFirstResponder]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (tableView == self.searchDisplayController.searchResultsTableView) { return [searDataList count]; } else return [dataList count]; } #pragma mark tableViewCell -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *customCellIdentifier = @"CustomCell"; static BOOL nibsRegistered = NO; if(!nibsRegistered){ UINib *nib = [UINib nibWithNibName:@"CustomCell" bundle:nil]; [tableView registerNib:nib forCellReuseIdentifier:customCellIdentifier]; nibsRegistered = YES; } CustomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:customCellIdentifier]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; MemorandumDataModel *dataModel = nil; if(tableView == self.searchDisCtrl.searchResultsTableView){ dataModel = [searDataList objectAtIndex:indexPath.row]; }else if(tableView == self.tableView){ dataModel = [dataList objectAtIndex:indexPath.row]; } cell.dataModel = dataModel; return cell; } -(void)setExtraCellLineHidden:(UITableView *)tableView{ UIView *view = [[UIView alloc]init]; view.backgroundColor = [UIColor clearColor]; [tableView setTableFooterView:view]; } -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{ return indexPath; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; EditViewController *editViewController = [storyboard instantiateViewControllerWithIdentifier:@"EditStoryboard"]; editViewController.dataModel = dataList[indexPath.row]; [self.navigationController pushViewController:editViewController animated:YES]; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 40; } -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ return YES; } -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleDelete; } -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ NSInteger i = indexPath.row; int backup_id ; if(editingStyle == UITableViewCellEditingStyleDelete){ if(tableView == self.searchDisCtrl.searchResultsTableView){ backup_id = [searDataList[i] backupID]; if([db deletInfoWithName:backup_id]){ [dataList removeObject:searDataList[indexPath.row]]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; } }else if(tableView == self.tableView){ backup_id = [dataList[i] backupID]; if([db deletInfoWithName:backup_id]){ [dataList removeObject:dataList[indexPath.row]]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; } } } } -(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{ return @"delete"; } #pragma mark - refresh -(void)setbeginRefreshing{ _refresh = [[UIRefreshControl alloc]init]; //刷新图形颜色 _refresh.tintColor = [UIColor lightGrayColor]; //刷新的标题 _refresh.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"]; [_refresh addTarget:self action:@selector(refreshTableviewAction:) forControlEvents:UIControlEventValueChanged]; self.refreshControl = _refresh; } -(void)refreshTableviewAction:(UIRefreshControl *)refreshs{ if(refreshs.refreshing){ refreshs.attributedTitle = [[NSAttributedString alloc]initWithString:@"正在刷新"]; [self performSelector:@selector(refreshData) withObject:nil afterDelay:2]; } } -(void) refreshData{ NSString *sysTime = nil; NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; sysTime = [formatter stringFromDate:[NSDate date]]; NSString *lastUpdated = [NSString stringWithFormat:@"上一次更新时间为 %@", [formatter stringFromDate:[NSDate date]]]; self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastUpdated]; [self.refreshControl endRefreshing]; [self updateData]; } - (void) updateData{ dataList = [NSMutableArray array]; for (int i=0;i<[[db searchInfoFromDB] count]; i ) { dataList[i] = [db searchInfoFromDB][i]; } [self.tableView reloadData]; } #pragma mark - UISearchBarDelegate - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ } -(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { [searchBar setShowsCancelButton:YES animated:YES]; // 修改UISearchBar右侧的取消按钮文字、颜色及背景图片. for (UIView *searchbuttons in [searchBar.subviews[0] subviews]){ if ([searchbuttons isKindOfClass:[UIButton class]]) { UIButton *cancelButton = (UIButton*)searchbuttons; [cancelButton setTitle:@"取消" forState:UIControlStateNormal]; // 修改文字颜色 [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; // 修改按钮背景 // [cancelButton setBackgroundColor:[UIColor whiteColor]]; // [cancelButton setBackgroundImage:nil forState:UIControlStateHighlighted]; } } } /*- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{ searchBar.showsCancelButton = YES; for (id cc in [searchBar subviews]) { if([cc isKindOfClass:[UIButton class]]){ UIButton *btn = (UIButton *)cc; btn.titleLabel.text = @"取消"; //[btn setTitle:@"取消" forState:UIControlStateNormal]; } } return YES; } - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{ [searchBar resignFirstResponder]; } */ #pragma mark - searchDisplay -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{ [searDataList removeAllObjects]; for (int i=0;i<[dataList count];i ){ NSString *str1 = [dataList[i]content]; NSString *str2 = _searchBar.text; if ([str1 rangeOfString:str2].location != NSNotFound) { [searDataList addObject:dataList[i]]; } } return YES; } @end
标签: iOS
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论