tableView点击下拉菜单
2016-09-06 20:14
246 查看
先给大家看看效果图吧
直接给大家看代码吧,注释都很详细
我在button的点击方法里注释了一段代码,解除注释后运行的效果是这样的
今天就到这里,祝大家开心
直接给大家看代码吧,注释都很详细
#import "ViewController.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> @property(nonatomic, strong)UITableView *tableView; @property(nonatomic, strong)NSMutableArray *sectionArray;//section标题 @property(nonatomic, strong)NSMutableArray *rowInSectionArray;//section中的cell个数 @property(nonatomic, strong)NSMutableArray *selectedArray;//是否被点击 @end @implementation ViewController -(void)loadView { [super loadView]; _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0 ,20 , self.view.frame.size.width, self.view.frame.size.height)style:UITableViewStylePlain]; _tableView.tableFooterView = [[UIView alloc]init]; _tableView.delegate = self; _tableView.dataSource = self; [self.view addSubview:_tableView]; } - (void)viewDidLoad { [super viewDidLoad]; _sectionArray = [NSMutableArray arrayWithObjects:@"标题1",@"标题2",@"标题3",@"标题4", nil];//每个分区的标题 _rowInSectionArray = [NSMutableArray arrayWithObjects:@"4",@"2",@"5",@"6", nil];//每个分区中cell的个数 _selectedArray = [NSMutableArray arrayWithObjects:@"0",@"0",@"0",@"0", nil];//这个用于判断展开还是缩回当前section的cell } #pragma mark cell的内容 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } cell.textLabel.text = _sectionArray[indexPath.section]; return cell; } #pragma mark cell的行数 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { //判断section的标记是否为1,如果是说明为展开,就返回真实个数,如果不是就说明是缩回,返回0. if ([_selectedArray[section] isEqualToString:@"1"]) { return [_rowInSectionArray[section]integerValue]; } return 0; } #pragma mark section的个数 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return _sectionArray.count; } #pragma cell的高度 -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 40; } #pragma mark - section内容 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { //每个section上面有一个button,给button一个tag值,用于在点击事件中改变_selectedArray[button.tag - 1000]的值 UIView *sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 40)]; sectionView.backgroundColor = [UIColor cyanColor]; UIButton *sectionButton = [UIButton buttonWithType:UIButtonTypeCustom]; sectionButton.frame = sectionView.frame; [sectionButton setTitle:[_sectionArray objectAtIndex:section] forState:UIControlStateNormal]; [sectionButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; sectionButton.tag = 1000 + section; [sectionView addSubview:sectionButton]; return sectionView; } #pragma mark button点击方法 -(void)buttonAction:(UIButton *)button { if ([_selectedArray[button.tag - 1000] isEqualToString:@"0"]) { // for (NSInteger i = 0; i < _sectionArray.count; i++) { // [_selectedArray replaceObjectAtIndex:i withObject:@"0"]; // [_tableView reloadSections:[NSIndexSet indexSetWithIndex:i] withRowAnimation:UITableViewRowAnimationFade]; // } //如果当前点击的section是缩回的,那么点击后就需要把它展开,是_selectedArray对应的值为1,这样当前section返回cell的个数就变为真实个数,然后刷新这个section就行了 [_selectedArray replaceObjectAtIndex:button.tag - 1000 withObject:@"1"]; [_tableView reloadSections:[NSIndexSet indexSetWithIndex:button.tag - 1000] withRowAnimation:UITableViewRowAnimationFade]; } else { //如果当前点击的section是展开的,那么点击后就需要把它缩回,使_selectedArray对应的值为0,这样当前section返回cell的个数变成0,然后刷新这个section就行了 [_selectedArray replaceObjectAtIndex:button.tag - 1000 withObject:@"0"]; [_tableView reloadSections:[NSIndexSet indexSetWithIndex:button.tag - 1000] withRowAnimation:UITableViewRowAnimationFade]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
我在button的点击方法里注释了一段代码,解除注释后运行的效果是这样的
今天就到这里,祝大家开心
相关文章推荐
- TableView的点击出现和关闭下拉菜单的实现
- iOS-tableView点击下拉菜单
- iOS 为自定义tableView添加button点击事件后获取其序号
- 点击tableView后面的button,通过响应者链条原理实现
- ios tableview/collectionView关于点击cell后 刷新cell
- 动态tableView中通过点击tableViewCell内部的一个控件找到indexPath
- iOS 输入框随键盘上移以及点击tableView回收键盘
- 【代码笔记】点击城市中的tableView跳转到旅游景点的tableView,下面会有“显示”更多。
- 解决TableView / ScrollView上的Menu问题(1滑出View区域还可点击2导致点击menu后View不能滑动)
- Swift3.0 tableView/CollectionView点击事件与手势的冲突
- tableview didselect去除点击效果
- gesture与tableview点击冲突问题
- IOS中点击TableView的Cell跳转
- tableView滑动出现删除按钮,不点击就返回崩溃
- 点击tableView的cell弹出tableView
- iOS 点击TableView 记录当前cell 当pop 回到页面时停留在之前点击的cell
- 点击UITableViewCell,下方展开一个tableView
- tableView点击更多及搜索栏
- ios7系统的tableview cell/tabfootview里面的uibutton点击时的高亮(hightlighted)显示
- IOS挖坑之路3 tableview造成整个界面第一次点击失效