iOS 不同按钮点击显示不同的tableview数据
2016-06-15 14:59
691 查看
点击按钮弹出列表选择 项目中常遇到,我用的是系统中的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"];
3、数据开始判断 标识
- (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;
}
那么下面就开始代码分析吧
还有一个更炫的在这里呢
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"];
3、数据开始判断 标识
- (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;
}
相关文章推荐
- iOS开发调试技巧总结(持续更新中)
- iOS开发技巧--禁止系统头文件被不小心修改
- iOS 视频直播
- iOS 封装本地化相关信息 NSLocale类介绍
- iOS导航栏颜色
- iOS下WebRTC音视频通话(一)
- iOS下WebRTC音视频通话(一)
- background-attachment:fixed在ios中失效
- iOS kvo 底层实现原理
- iOS 使用XMPP框架开发IM聊天模块,实现简单的文字聊天
- iOS 开发使用七牛云存储与自己产品的配合
- ios开发之----复制和粘贴
- 【iOS开发】NSDictionary 判空处理
- iOS --二维码
- iOS设计模式之代理模式
- iOS-设置导航栏颜色(iOS8+)
- iOS 单元测试4- 单元测试编码规范
- iOS 单元测试3- Singleton如何测试
- iOS 单元测试2- OCMock常见使用方式
- iOS 简单的视频直播功能开发(实时视音频流录制编码+RTMP传输+实时拉流解码播放)