tableView实现二级菜单
来源:互联网 发布:淘宝店铺怎么加入淘客 编辑:程序博客网 时间:2024/04/30 02:31
代码很简单,直接上代码:
#import "ViewController.h"#import "MainCell.h"#import "SecondCell.h"#define ExpandCount 3static NSString *mainCell = @"mainCell";static NSString *secondCell = @"secondCell";@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>@property (strong, nonatomic) IBOutlet UITableView *tableView;@property (assign, nonatomic) BOOL isExpand; //是否展开@property (strong, nonatomic) NSIndexPath *selectedIndexPath;//展开的cell的下@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; _tableView.delegate=self; _tableView.dataSource=self; [_tableView registerNib:[UINib nibWithNibName:@"MainCell" bundle:nil] forCellReuseIdentifier:mainCell]; [_tableView registerNib:[UINib nibWithNibName:@"SecondCell" bundle:nil] forCellReuseIdentifier:secondCell];}//返回tableview中cell的个数-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (self.isExpand == YES) { return 1 + ExpandCount; } return 1;}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1;}//设置 cell的样式-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row == 0) { MainCell *cell = [tableView dequeueReusableCellWithIdentifier:mainCell forIndexPath:indexPath]; if (cell == nil) { cell = [[MainCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:mainCell]; } return cell; } else{ if (self.isExpand && self.selectedIndexPath.row < indexPath.row && indexPath.row <= self.selectedIndexPath.row + ExpandCount) { // Expand cell SecondCell *cell = [tableView dequeueReusableCellWithIdentifier:secondCell forIndexPath:indexPath]; if (!cell) { cell = [[SecondCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:secondCell]; } return cell; } return nil; }}//返回cell的高度-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 50.0f;}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}//当cell被选择(被点击)时调用的函数-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row == 0) { NSLog(@"点击一级cell"); if (!self.selectedIndexPath) { self.isExpand = YES; self.selectedIndexPath = indexPath; NSArray *arr = [self indexPathsForExpandRow:indexPath.row]; [tableView beginUpdates]; [tableView insertRowsAtIndexPaths:arr withRowAnimation:(UITableViewRowAnimationBottom)]; [tableView endUpdates]; }else{ if (self.isExpand) { if (self.selectedIndexPath == indexPath) { self.isExpand = NO; NSArray *arr = [self indexPathsForExpandRow:indexPath.row]; [tableView beginUpdates]; [tableView deleteRowsAtIndexPaths:arr withRowAnimation:(UITableViewRowAnimationBottom)]; [tableView endUpdates]; self.selectedIndexPath = nil; } } } }else{ NSLog(@"点击二级cell"); }}- (NSArray *)indexPathsForExpandRow:(NSInteger)row { NSMutableArray *indexPaths = [NSMutableArray array]; for (int i = 1; i <= ExpandCount; i++) { NSIndexPath *idxPth = [NSIndexPath indexPathForRow:row + i inSection:0]; [indexPaths addObject:idxPth]; } return [indexPaths copy];}@end
注:MainCell
SecondCell
分别是一级cell和二级cell。
转载请注明出处,万分感谢!
1 0
- tableView实现二级菜单
- Cocos2dx学习之---TableView实现二级菜单
- 二级菜单实现
- ListView实现二级菜单
- 实现二级Menu菜单
- jquery实现二级菜单
- 实现伸缩二级菜单
- CSS3实现二级菜单
- css实现二级菜单
- android 二级菜单实现
- 实现伸缩二级菜单
- HTML实现二级菜单
- 二级菜单的实现
- 简单二级菜单实现
- 二级菜单的实现
- CSS实现二级菜单
- swing 菜单+右键菜单+二级菜单实现
- 利用tableView实现下拉菜单
- 文本特征提取_01:词项文档矩阵
- 《数据结构和算法》之双向链表
- PAT算法笔记(十五)————D进制的A+B
- 响应式网格(栅格化)布局
- Linux进程间通信(IPC)之综述
- tableView实现二级菜单
- ORA-01841: (完整) 年份值必须介于 -4713 和 +9999 之间, 且不为 0情况解决
- NoSQL中的行存储与列存储
- 如何在Ubuntu 14.04上安装Elasticsearch、Logstash以及Kibana(即ELK堆栈)
- 6种负载均衡算法
- JAVA并发编程——守护线程(Daemon Thread)
- shell命令date获取时间精确到毫秒
- 老男孩全程就业指导视频教程
- U盘安装CentOS7查看u盘设备名称的命令