点击按钮弹出列表选择 项目中常遇到,我用的是系统中的tableview 原理就是先创建好列表的UI,将其隐藏,在点击事件中操作的时候在显现出来 同时刷新列表 这里用tag标记值来识别你刷新的是哪一个数据源哪一个按钮执行的操作,好了 原理就是这样的 那么下面就开始代码分析吧
还有一个更炫的在这里呢
1、首先创建两个数组来包含列表中的数据源
@interface BLEViewController ()
{
NSArray *array,*array2;
int flag;
}
@property (weak,nonatomic)IBOutlet UIButton *Scan;
@property (weak,nonatomic)IBOutletUIButton *Print;
@end
@implementation BLEViewController
- (void)viewDidLoad {
[superviewDidLoad];
2、因为我用的是xib创建的所以先将其隐藏
_tableV.hidden =YES;
array = @[@"时间地方",@"氨基酸的",@"AIDS",@"爱学",
@"比屋而封",@"啊防护等级撒",@"阿斯顿",@"卡戴珊覅",
@"你骄傲的",@"不雅",@"额外调查",@"卡丁车"];
array2 =@[@"阿萨德地方",@"束带结发的",@"iun",@"爱那就",
@"比屋觉得女警爱封",@"而无办法级撒",@"我放假饿",@"i32fj",
@"2哦哦诶积分",@"饿哦妇女",@"爱发呆呢查",@"就i"];
- (IBAction)openDoor:(UIButton *)sender {
flag =1;
_tableV.hidden =NO;
[_tableVreloadData];
[UIViewanimateWithDuration:0.3fanimations:^{
self.tableV.frame =CGRectMake(0, -self.view.frame.size.height,self.view.frame.size.width,self.view.frame.size.height);
} completion:^(BOOL finished) {
self.tableV.frame =CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height);
}];
}
- (IBAction)OOO:(UIButton *)sender {
flag =2;
_tableV.hidden =NO;
[_tableVreloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
returnflag ==1 ?array.count:array2.count;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (flag) {
case1:
[_ScansetTitle:[arrayobjectAtIndex:indexPath.row]forState:(UIControlStateNormal)];
break;
case2:
[_PrintsetTitle:[array2objectAtIndex:indexPath.row]forState:(UIControlStateNormal)];
break;
default:
break;
}
_tableV.hidden =YES;
}
4、数据开始赋值
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
staticNSString *cellId =@"peripheral";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:cellId];
if (cell ==nil) {
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellId];
}
switch (flag) {
case1:
cell.textLabel.text =array[indexPath.row];
break;
case2:
cell.textLabel.text =array2[indexPath.row];
break;
default:
break;
}
return cell;
}