利用tableView的分组功能进行qq页面设计


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];  

    //加载数据

    [self _loadData]; 

    //加载视图

    [self _loadTableView];   

}


#pragma mark - 加载数据

- (void)_loadData{ 

    //获取文件所在路径--数组

    NSString * path=[[NSBundle mainBundle]pathForResource:@"friends.plist" ofType:nil];

    self.dataArray=[NSArray arrayWithContentsOfFile:path];

}



#pragma mark - 加载tableView

- (void)_loadTableView{

    

    UILabel * titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 60)];

    titleLabel.text=@"QQ列表";

    titleLabel.textColor=[UIColor redColor];

    titleLabel.font=[UIFont boldSystemFontOfSize:30];

    titleLabel.textAlignment=NSTextAlignmentCenter;

    [self.view addSubview:titleLabel];


    

    UITableView * tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, self.view.frame.size.height-80) style:UITableViewStyleGrouped];

    tableView.delegate=self;

    tableView.dataSource=self;

    tableView.rowHeight=70;//cell的高度

    tableView.sectionFooterHeight=0;//设置尾部高度为0

    self.tableView=tableView;

    

    tableView.backgroundColor=[UIColor clearColor];

    [self.view addSubview:tableView];

    

}



#pragma mark - UITableViewDataSource

//返回行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    //如果开关已经打开,则返回实际的数据行数

    if(self.staticDic[@(section)]){

    

        NSDictionary * temDic=self.dataArray[section];

        NSArray * section=[temDic valueForKey:@"friends"];

        return section.count;

    }

    return 0;

}


//返回cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    static NSString * identy=@"table";

    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:identy];

    if(!cell){

        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identy];

        

    }

    cell.backgroundColor=[UIColor clearColor];

    NSDictionary * temDic=self.dataArray[indexPath.section];

    NSArray * temArray=temDic[@"friends"];

    cell.textLabel.text=temArray[indexPath.row];

    cell.detailTextLabel.text=@"在线";


    NSString * imageName=[NSString stringWithFormat:@"head%i",arc4random_uniform(7)+1];

    cell.imageView.image=[UIImage imageNamed:imageName];

    return cell;

    

}


//返回分组数量

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return self.dataArray.count;

}


#pragma mark - UITableViewDelegate

#pragma mark - 设置头部高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 50;

}


#pragma mark - 自定义头部

//返回header

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    //为头部设置可以重复利用的view

    static NSString * identy=@"headerfoot";

    UITableViewHeaderFooterView * hf=[tableView dequeueReusableHeaderFooterViewWithIdentifier:identy];

    if(hf==nil){

        hf=[[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:identy];

       

        UIView * headView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 48)];

        headView.tag=1111111;

        headView.backgroundColor=[UIColor colorWithWhite:0.2 alpha:0.1];


        //view上添加相同大小的按钮

        UIButton * button=[[UIButton alloc]initWithFrame:headView.bounds];

        [button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];

        

        //添加三角形图标

        UIImageView * imageView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 40, 40)];

//        imageView.userInteractionEnabled=YES;

        imageView.image=[UIImage imageNamed:@"disclosure"];

        imageView.tag=1001;

        

        //添加组名

        UILabel * titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(50, 5, headView.frame.size.width-50, 40)];

        titleLabel.textAlignment=NSTextAlignmentCenter;

        titleLabel.tag=103;

        

        [headView addSubview:titleLabel];

        [headView addSubview:imageView];

        [headView addSubview:button];

        [hf.contentView addSubview:headView];


    }

    //重新获取headView

    UIView * headView=[hf viewWithTag:1111111];

    //获取button,设置标题

    UIButton * button=[headView.subviews lastObject];

    button.tag=section;

    //添加分组标题

    NSDictionary * temDic=self.dataArray[section];

    UILabel * temLabel=(UILabel *)[hf viewWithTag:103];

    temLabel.text=temDic[@"group"];

    

    //将刷新的imageView加入到字典

    UIImageView * imageView=(UIImageView *)[headView viewWithTag:1001];

    [self.imageDic setObject:imageView forKey:@(section)];


    //消除重复利用图片状态

    [self initImageStateWithCondition:self.staticDic[@(section)] andSection:section];

    return hf;

}


#pragma mark - clickAction

- (void)clickAction:(UIButton *)button{

    

    NSInteger section=button.tag;

    //判断是否打开--字典里面信息是否为空

    BOOL isOpen=self.staticDic[@(section)]!=nil;

    //如果已经打开,则移除里面的信息

    //如果没有打开,则加载信息

    if(isOpen){

        [self.staticDic removeObjectForKey:@(section)];

    }else{

        [self.staticDic setObject:@(1) forKey:@(section)];

    }

    

    //折叠动画--刷新数据

    NSIndexSet * set=[NSIndexSet indexSetWithIndex:section];

    [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationFade];

   

    UIImageView * image=self.imageDic[@(section)];

    [self initImageStateWithCondition:isOpen andSection:section];

    

    //设置动画,重新判断状态

    [UIView animateWithDuration:0.5 animations:^{

        if(!isOpen){

            image.transform=CGAffineTransformMakeRotation(M_PI_2);

        }else{

            image.transform=CGAffineTransformIdentity;//恢复原来的状态

        }

    }]; 

}


//消除图片重复利用--刷新后先判断图片状态把图片重新快速置为所对应状态,,然后再执行动画

- (void)initImageStateWithCondition:(BOOL)condition andSection:(NSInteger)section{


    UIImageView * image=self.imageDic[@(section)];

    if(condition){

        image.transform=CGAffineTransformMakeRotation(M_PI_2);

    }else{

        image.transform=CGAffineTransformIdentity;//恢复原来的状态

    }

}



//点击cell

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"------->联系人信息");

}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值