UITableView cell下拉菜单的做法

//
//  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




效果如下图


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值