uitableview 和UISearchBar 下拉提示结合使用
2013-06-23 01:18 甘超波 阅读(4030) 评论(0) 编辑 收藏 举报自定cell的代码
餐厅的实体和餐厅对应控件的frame
#import <Foundation/Foundation.h> @class RestaurantFrame; @interface Restaurant : NSObject @property(nonatomic,copy)NSString *restaurantIcon; @property(nonatomic,copy)NSString *restaurantTitle; @property(nonatomic,copy)NSString *restaurantSubTitle; @property(nonatomic,retain) RestaurantFrame *frame;//初始化内容的时候也要得到餐厅中控件的位置和内容 +(id)RestaturantImage:(NSString *)image Title:(NSString *)title subTitle:(NSString *)subtitle; @end @implementation Restaurant +(id)RestaturantImage:(NSString *)image Title:(NSString *)title subTitle:(NSString *)subtitle{ Restaurant *res=[[Restaurant alloc] init]; res.restaurantIcon=image; res.restaurantTitle=title; res.restaurantSubTitle=subtitle; res.frame=[[RestaurantFrame alloc] initWithRestaurant:res]; return [res autorelease]; } - (void)dealloc { [_restaurantIcon release]; [_restaurantTitle release]; [_restaurantSubTitle release]; [_frame release]; [super dealloc]; } @end
餐厅对应的cell的位置
#import "Restaurant.h" #define KPadding 10 #define kImageWidth 100 #define KImageHeight 100 #define KTitleFont 15 #define kSubFont 12 @implementation RestaurantFrame -(id)initWithRestaurant:(Restaurant *)res{ if(self=[super init]){ CGFloat width=[UIScreen mainScreen].bounds.size.width ; //通过内容算大小 CGFloat imagex=KPadding; CGFloat imgagey=KPadding; CGFloat imgagew=kImageWidth; CGFloat imgageh=KImageHeight; _restaurantImageFram=CGRectMake(imagex,imgagey,imgagew,imgageh); CGFloat labelx=imagex+KPadding+imgagew; CGFloat labely=imgagey; CGFloat labelw=width-imgagew-imagex-KPadding*2; CGFloat labelh=[UIFont systemFontOfSize:KTitleFont].lineHeight; _restaurantLabelFram=CGRectMake(labelx,labely,labelw,labelh); CGFloat sublabelx=labelx; CGFloat sublabely=imgagey+labelh+KPadding; CGFloat sublabelw=labelw; CGFloat sublabelh= [res.restaurantSubTitle sizeWithFont:[UIFont systemFontOfSize:kSubFont] constrainedToSize:CGSizeMake(sublabelw, 10000) lineBreakMode:NSLineBreakByCharWrapping].height; _resSubTitle=CGRectMake(sublabelx, sublabely, sublabelw, sublabelh); _cellHeight=imgageh; } return self; } @end
cell内部初始化的控件的位置
#import <UIKit/UIKit.h> @class Restaurant; @interface UIMyCell : UITableViewCell //把餐厅信息设置到cell @property(nonatomic,retain)Restaurant *rest; @property(nonatomic,readonly)UIImageView *Resimageview; //@property(nonatomic,readonly)UILabel *titleLabel; @end #import "UIMyCell.h" #import "Restaurant.h" #import "RestaurantFrame.h" @interface UIMyCell() { UILabel *_subTitle; UILabel *_TitleLabel; } @end @implementation UIMyCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code UIImageView *imageview=[[UIImageView alloc] init]; [self.contentView addSubview:imageview]; _Resimageview=imageview; [imageview release]; UILabel *TitleLabel=[[UILabel alloc] init]; TitleLabel.backgroundColor=[UIColor clearColor]; TitleLabel.font=[UIFont systemFontOfSize:15]; [self.contentView addSubview:TitleLabel]; _TitleLabel=TitleLabel; [TitleLabel release]; UILabel *subTitle=[[UILabel alloc] init]; subTitle.backgroundColor=[UIColor clearColor]; subTitle.font=[UIFont systemFontOfSize:12]; subTitle.numberOfLines=0; [self.contentView addSubview:subTitle]; _subTitle=subTitle; [subTitle release]; } return self; } -(void)setRest:(Restaurant *)rest{ if(_rest!=rest){ _rest=[rest retain]; _Resimageview.image=[UIImage imageNamed:rest.restaurantIcon];//设置默认图片 _TitleLabel.text=rest.restaurantTitle; _subTitle.text=rest.restaurantSubTitle; } } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } - (void)dealloc { self.rest=nil; [super dealloc]; } -(void)layoutSubviews{ [super layoutSubviews]; _Resimageview.frame=self.rest.frame.restaurantImageFram; _TitleLabel.frame=self.rest.frame.restaurantLabelFram; _subTitle.frame=self.rest.frame.resSubTitle; } @end
第一步在主视图控制器中 加载一个UISearchBar 和UITableview
1 -(void)Inittableview{ 2 3 4
//初始化主view中tableview 加载到self.view上 5 CGFloat viewx=0; 6 CGFloat viewy=0; 7 CGFloat vieww=self.view.bounds.size.width; 8 CGFloat viewH=self.view.bounds.size.height-44; 9 UITableView *view=[[UITableView alloc] initWithFrame:CGRectMake(viewx,viewy,vieww,viewH)]; 10 view.delegate=self; 11 view.dataSource=self; 12 _tableview=view; 13 UIView *bgview=[[[UIView alloc] init] autorelease]; 14 bgview.backgroundColor=[UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:0.8]; 15 view.backgroundView=bgview; 16 17 [self.view addSubview:view]; 18 [view release]; 19 20 //初始化搜索框,在self.view上 21 UISearchBar *searchbar=[[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0,320, kSearchSize)] autorelease]; 22 searchbar.placeholder=@"please enter keyword"; 23 searchbar.autocorrectionType=UITextAutocapitalizationTypeNone; 24 searchbar.delegate=self;//设置代理 25 26 //添加下拉框 27 SearchViewController *searchVC=[[[SearchViewController alloc] initWithStyle:UITableViewStylePlain] autorelease]; 28 [searchVC .view setFrame:CGRectMake(30 , 40, 200, 0)];//0加载的时候高度为0隐藏 29 30 [self.view addSubview:searchVC.view]; 31 _searchVC=searchVC; 32 [self addChildViewController:searchVC]; 33 view.tableHeaderView=searchbar; 34 [searchbar release]; 35 36 37 }
tableview 的代理方法 cell是自定义的
#pragma mark -tableview datesource -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ returnself.mydata.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellIndentify=@"mycell"; UIMyCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIndentify]; if(cell==nil){ cell=[[[UIMyCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIndentify] autorelease]; } Restaurant *res=self.mydata[indexPath.row]; cell.rest=res; return cell; } #pragma mark -tableview delegate -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ Restaurant *rest=self.mydata[indexPath.row]; return rest.frame.cellHeight; }
自定义下拉是一个uitableviewcontroller
// // SearchViewController.h // MyTestDemo001 // // Created by ganchaobo on 13-6-22. // Copyright (c) 2013年 ganchaobo. All rights reserved. // #import <UIKit/UIKit.h> @interface SearchViewController : UITableViewController @property(nonatomic,assign)NSMutableArray *mydate; @end #import <QuartzCore/QuartzCore.h> @interface SearchViewController () @end #import "Restaurant.h" @implementation SearchViewController - (void)viewDidLoad { [super viewDidLoad];
//初始化的设置隐藏的tableiew的边框 self.tableView.layer.borderWidth=1; self.tableView.layer.borderColor=[UIColor blackColor].CGColor; // Do any additional setup after loading the view. } #pragma mark -delegate source -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.mydate==nil?3:self.mydate.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellIndentify=@"SearchViewController"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIndentify]; if(cell==nil){ cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentify] autorelease]; } if(self.mydate!=nil){//第一加载为空。第二次有值 Restaurant *res=self.mydate[indexPath.row]; cell.textLabel.text=res.restaurantTitle; } return cell; } -(void)setMydate:(NSMutableArray *)mydate{ _mydate=mydate;//给外界设置。 [self.tableView reloadData]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
在主窗口中设置下拉的uitableviewcontroller在view中隐藏和现实的位置
-(void)setSearchControllerHidden:(BOOL)hidden{ NSInteger height=hidden?0:180; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.1f]; _searchVC.view.frame=CGRectMake(30, 40, 200, height); [UIView commitAnimations]; }
searchbar 的代理方法 用的时候两个数组来完成,先清楚一个,然后遍历另一个数组
#pragma mark -searchbar - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ [self SearchData:searchText]; } //search -(void)SearchData:(NSString*)searchText{ [self.mydata removeAllObjects];//清除原来的 if(searchText.length!=0){ for (Restaurant *res in self.searchdata) { NSRange range=[res.restaurantTitle rangeOfString:searchText]; if(range.length>0){ [self.mydata addObject:res];//填充匹配的数据 //现实下拉 [self setSearchControllerHidden:NO]; } } _searchVC.mydate=_mydata; } else{ for (Restaurant *res in self.searchdata) { [self.mydata addObject:res];//填充匹配的数据 } [self setSearchControllerHidden:NO]; } [_tableview reloadData]; } -(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{ searchBar.text=@""; } -(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{ searchBar.showsCancelButton=YES; for (id cc in searchBar.subviews) { if([cc isKindOfClass:[UIButton class]]){ UIButton *btn=(UIButton *)cc; [btn setTitle:@"取消" forState:UIControlStateNormal]; } } return YES; } -(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{ searchBar.text=@""; [self SearchData:searchBar.text]; [searchBar resignFirstResponder]; //当点击取消按钮的时候隐藏 [self setSearchControllerHidden:YES]; }
目前我正在专注NLP,请立刻加微信/QQ号 546611623, 免费送你原创《NLP高级执行师》高清视频