UITableView cell下拉菜单的做法
2015-08-10 21:04
543 查看
// // TableViewController.m // UITableView cell下拉 // // Created by lanou3g on 15/8/10. // Copyright (c) 2015年 lanou3g. All rights reserved. // #import "TableViewController.h" @interface TableViewController () @property(nonatomic,strong) UIButton *button ; //模拟cell的button @property(nonatomic,strong) NSArray *sectionArray;//section标题 @property(nonatomic,strong) NSArray *rowArray;//模拟数据源 @property(nonatomic,strong) NSArray *rowArray2;//模拟数据源 @property(nonatomic,strong) NSMutableDictionary* dic;//用来判断分组展开与收缩的 @end @implementation TableViewController - (void)viewDidLoad { [super viewDidLoad]; //使taleView 没有下拉线 self.tableView.showsVerticalScrollIndicator = NO; _sectionArray = @[@"体育",@"音乐",@"影视",@"生活",@"美食"]; _rowArray = @[@"体育",@"音乐",@"影视",@"生活",@"美食"]; _rowArray2 = @[@"七里香",@"稻花香",@"女儿红"]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return [_sectionArray count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section %2 == 0) { return [_rowArray count]; }else{ return [_rowArray2 count]; } } //展示表头 -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ //将button作为表头 self.button = [UIButton buttonWithType:UIButtonTypeCustom]; self.button.tag = section; [self.button setTitle:[NSString stringWithFormat:@"%@",_sectionArray[section]] forState:UIControlStateNormal]; self.button.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:0.7]; [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; return self.button; } //表头高度 -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 44; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if ([_dic objectForKey:[NSString stringWithFormat:@"%ld",indexPath.section]]) { return 44; } return 0; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; if (indexPath.section %2 == 0) { cell.textLabel.text = [NSString stringWithFormat:@"第%ld个cell : %@",indexPath.row+1,_rowArray[indexPath.row]]; }else{ cell.textLabel.text = [NSString stringWithFormat:@"第%ld个cell : %@",indexPath.row+1,_rowArray2[indexPath.row]]; } //决定子视图的显示范围 cell.clipsToBounds = YES; return cell; } //展开收缩cell -(void)buttonAction:(UIButton*)sender{ NSInteger didSection = sender.tag; if (!_dic) { _dic = [[NSMutableDictionary alloc]init]; } NSString *key = [NSString stringWithFormat:@"%ld",didSection]; if (![_dic objectForKey:key]) { [_dic setObject:@"" forKey:key]; }else{ [_dic removeObjectForKey:key]; } [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:didSection] withRowAnimation:UITableViewRowAnimationFade]; } @end
效果如下图
相关文章推荐
- UITableView 实现汽车品牌(demo)
- UITableView 基本使用方法总结
- UITableView一些易混属性和方法
- UITableView的详细讲解
- (转)UITableViewController重要配置方法和Delegate
- UITableView详细用法,UITableView指南,UITableView详细教程
- ios隐藏uitableview下面多余的tableview的线条
- 定制UITableViewCell的事件响应处理
- ios关于tableView的重用
- IOS UITableView 实现LOL数据展示
- iOS- UITableView
- 仿QQ控件图片下拉放大效果
- QQ分组
- 关于SearchaBar的bookMarks(代理方式实现)
- UITableView
- tableviewcell 侧滑删除 点击返回按钮 程序崩溃 [ tableView:canEditRowAtIndexPath:]:message sent to deallocated insta
- UIScrollView的使用大全--contentsize,contentInset和contentInset
- 关于TableViewCell 自适应(文字自适应)
- UITableView 实现异步加载图片之改进
- 解决IOS中UITableViewStylePlain 时对headerview的隐藏