您的位置:首页 > 其它

tableView点击下拉菜单

2016-09-06 20:14 246 查看
先给大家看看效果图吧



直接给大家看代码吧,注释都很详细

#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的点击方法里注释了一段代码,解除注释后运行的效果是这样的



今天就到这里,祝大家开心
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: