类似于QQ列表展开和收起的列表

来源:互联网 发布:js替换file属性input 编辑:程序博客网 时间:2024/05/16 18:32
#import "ViewController.h"#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width@interface ViewController () <UITableViewDataSource,UITableViewDelegate>@property (nonatomic,strong)UITableView *tabelView;@property (nonatomic,strong)NSMutableArray *array;@property (nonatomic,strong)NSMutableDictionary *dict;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    //创建tableView    self.tabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];    self.tabelView.delegate = self;    self.tabelView.dataSource = self;    [self.view addSubview:self.tabelView];    //记录的字典    self.dict = [NSMutableDictionary dictionary];    //每组的标题    self.array = [[NSMutableArray alloc] init];    //去除tableView多余的横线    self.tabelView.tableFooterView = [[UIView alloc] init];        for(int i=0;i<5;i++){        NSString *str = [NSString stringWithFormat:@"第%d组",i];        [self.array addObject:str];    }}//每组的组头- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];    view.backgroundColor = [UIColor cyanColor];    view.userInteractionEnabled = YES;    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, (44-20)/2, 100, 20)];    label.text = self.array[section];    [view addSubview:label];    //view的tag就等于section 代表点击了哪一个组    view.tag = section;    [view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(display1:)]];    return view;}- (void)display1:(UITapGestureRecognizer *)g{        //将点击了哪一组转换成字符串    NSString *str = [NSString stringWithFormat:@"%ld",g.view.tag];    //从字典里面以第几组为key取出状态值    //如果状态值为0,代表关闭    if([self.dict[str] integerValue] == 0){        [self.dict setObject:@(1) forKey:str];    }    //如果状态值为不为0,代表展开    else{        [self.dict setObject:@(0) forKey:str];    }    //记得一定要刷新tabelView,不然没有效果    [self.tabelView reloadData];}- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{    return 44;}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 5;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    /*调用tableView的reloadData方法会重新调用这个方法     从而从字典里面取出相应组对应的状态码,从而判断是需要展开还是收起    */    NSString *str = [NSString stringWithFormat:@"%ld",section];    //将点击了哪一组转换成字符串    if([self.dict[str] integerValue] == 1){        //如果状态值为等于1,代表需要展开返回真正的多少个Cell        return 5;    }else{        //如果状态值为等于0,代表需要收起返回0        return 0;    }}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *cellId = @"cell";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];    if(cell == nil){        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];    }    cell.textLabel.text = @"每一行";    return cell;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];}@end
复制代码

运行效果

我在公司项目做的效果,我感觉后面的箭头还是做的不错的,哈哈,大家可以下载我们应用看看云收益Pro(iOS版本哦)

0 0
原创粉丝点击