抽屜效果的實現:
首先是寫一個自定義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