uitableview 和UISearchBar 下拉提示结合使用

时间:2022-07-21 10:44:20

自定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*;
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, ) 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:];
[self.contentView addSubview:TitleLabel];
_TitleLabel=TitleLabel;
[TitleLabel release]; UILabel *subTitle=[[UILabel alloc] init];
subTitle.backgroundColor=[UIColor clearColor];
subTitle.font=[UIFont systemFontOfSize:];
subTitle.numberOfLines=; [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

 -(void)Inittableview{

   //初始化主view中tableview 加载到self.view上
CGFloat viewx=;
CGFloat viewy=;
CGFloat vieww=self.view.bounds.size.width;
CGFloat viewH=self.view.bounds.size.height-;
UITableView *view=[[UITableView alloc] initWithFrame:CGRectMake(viewx,viewy,vieww,viewH)];
view.delegate=self;
view.dataSource=self;
_tableview=view;
UIView *bgview=[[[UIView alloc] init] autorelease];
bgview.backgroundColor=[UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:0.8];
view.backgroundView=bgview; [self.view addSubview:view];
[view release]; //初始化搜索框,在self.view上
UISearchBar *searchbar=[[[UISearchBar alloc] initWithFrame:CGRectMake(, ,, kSearchSize)] autorelease];
searchbar.placeholder=@"please enter keyword";
searchbar.autocorrectionType=UITextAutocapitalizationTypeNone;
searchbar.delegate=self;//设置代理 //添加下拉框
SearchViewController *searchVC=[[[SearchViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
[searchVC .view setFrame:CGRectMake( , , , )];//0加载的时候高度为0隐藏 [self.view addSubview:searchVC.view];
_searchVC=searchVC;
[self addChildViewController:searchVC];
view.tableHeaderView=searchbar;
[searchbar release]; }

tableview 的代理方法  cell是自定义的

#pragma mark -tableview datesource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return ;

}

-(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=;
self.tableView.layer.borderColor=[UIColor blackColor].CGColor;
// Do any additional setup after loading the view. } #pragma mark -delegate source
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return ;
} -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.mydate==nil?: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?:;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.1f]; _searchVC.view.frame=CGRectMake(, , , 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!=){ for (Restaurant *res in self.searchdata) {
NSRange range=[res.restaurantTitle rangeOfString:searchText];
if(range.length>){
[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];
}