iOS开发--抽屉
抽屉效果的实现:
首先是写一个自定义UIView,在这个自定义View上添加一个UITableView,UITableView的大小一般与自定义View的大小相同,再将自定义View添加到视图控制器ViewController上,在点击UITableView的单元格cell时,根据不用cell的位置,跳转到不同的界面。下面是具体步骤:
.h文件:
#import
#import "BasicView.h"
typedefvoid(^Block)(NSIndexPath*);
@interface DrawerView:BasicView
@property(nonatomic,copy)Block block;
@end
.m文件:
#import "DrawerView.h"
@interface DrawerView()
@property(nonatomic,retain)UITableView *drawerTableView;
@property(nonatomic,retain)NSArray *picArray;
@property(nonatomic,retain)NSArray *titleArray;
@end
@implementationDrawerView
-(void)dealloc
{
[_drawerTableViewrelease];
[_picArrayrelease];
[_titleArrayrelease];
[superdealloc];
}
-(instancetype)initWithFrame:(CGRect)frame
{
self=[superinitWithFrame:frame];
if(self){
// NSLog(@"🍊");
self.picArray=@[@"2",@"3",@"4",@"5",@"6",@"7",@"8"];
//各个单元格名称数组
self.titleArray=@[@"类别",@类别",@"类别",@"类别",@"类别",@"类别",@"类别"];
[selfcreateTableView];
}
returnself;
}
-(void)createTableView{
UIImageView*imageView=[[UIImageViewalloc]initWithFrame:self.bounds];
imageView.image=[UIImageimageNamed:@"20160119093613_FaTtz.thumb.224_0"];
[selfaddSubview:imageView];
[imageViewrelease];
self.drawerTableView=[[UITableViewalloc]initWithFrame:CGRectMake(0,self.bounds.size.height/10,self.bounds.size.width,self.bounds.size.height)style:UITableViewStyleGrouped];
self.drawerTableView.dataSource=self;
self.drawerTableView.delegate=self;
self.drawerTableView.backgroundColor=[UIColorclearColor];
self.drawerTableView.scrollEnabled=NO;
self.drawerTableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
[selfaddSubview:self.drawerTableView];
[_drawerTableViewrelease];
[self.drawerTableViewregisterClass:[UITableViewCellclass]forCellReuseIdentifier:@"drawerCell"];
// NSLog(@"🍎");
}
-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section
{// NSLog(@"🍑");
returnself.picArray.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:@"drawerCell"];
cell.backgroundColor=[UIColorclearColor];
cell.textLabel.textColor=[UIColorwhiteColor];
;
cell.imageView.image=[UIImageimageNamed:self.picArray[indexPath.row]];
cell.textLabel.text=self.titleArray[indexPath.row];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
returncell;
}
-(CGFloat)tableView:(UITableView*)tableViewheightForRowAtIndexPath:(NSIndexPath*)indexPath
{
return[UIScreenmainScreen].bounds.size.height/10;
}
-(void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
self.block(indexPath);
}
@end