//声明文件(.h)里的代码
//
//注意:尖括号,两个以上,要用逗号隔开
#import <UIKit/UIKit.h>
#import "Masonry.h"
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
@end
//实现文件(.m)中的代码:
//
//点击一个排序的按钮,触发下拉菜单的加载
//排序按钮点击事件
- (void)orderByBuildTimeButtonClick:(id)sender {
//创建tableView下拉菜单
orderTableView=[[UITableView alloc]initWithFrame:CGRectMake(245, 65, 130, 104) style:UITableViewStyleGrouped];
orderTableView.dataSource=self;//重点
orderTableView.delegate=self;//重点
[self.view addSubview:orderTableView];
//自动布局
[orderTableView mas_updateConstraints:^(MASConstraintMaker *make){
make.top.equalTo(self.view).with.offset(65);
make.right.equalTo(self.view).with.offset(0);
make.width.mas_equalTo(130);
make.height.mas_equalTo(121);
}];
}
//返回cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//cell的基本设置
NSInteger row=[indexPath row]; //通过 [indexPath row] 遍历数组
static NSString *CellIdentifier=@"cell";// 定义个静态字符串 为了防止与其他类的tableivew重复
ordercell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];//定义cell 的复用性 当处理大量数据时减少内存开销
if(ordercell==nil)
{
ordercell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
ordercell.textLabel.font=[UIFont systemFontOfSize:13];
ordercell.textLabel.textColor=[UIColor grayColor];
//每个cell的文本不相同时可以这样设置
switch (row) {
case 0:
ordercell.textLabel.text=@"按创建时间排序";
break;
case 1:
ordercell.textLabel.text=@"按更新时间排序";
break;
case 2:
ordercell.textLabel.text=@"按数目排序";
break;
}
}
//orderTableView不可滚动。
orderTableView.scrollEnabled=NO;
return ordercell;
}
//每个cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 44;
}
//返回section数
-(NSInteger)numberOfSectionInTableView:(UITableView *)tableView{
return 1;
}
//返回cell的行数,即多少个cell
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 3;
}
//设置tableView的段头
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 0.1;
}
排序按钮的位置:
设置段头前:
设置段头后: