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

来源:互联网 发布:安卓mac修改器apk 编辑:程序博客网 时间:2024/05/16 14:12


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];  

    //加载数据

    [self_loadData]; 

    //加载视图

    [self_loadTableView];   

}


#pragma mark - 加载数据

- (void)_loadData{ 

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

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

    self.dataArray=[NSArrayarrayWithContentsOfFile:path];

}



#pragma mark - 加载tableView

- (void)_loadTableView{

    

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

    titleLabel.text=@"QQ列表";

    titleLabel.textColor=[UIColorredColor];

    titleLabel.font=[UIFontboldSystemFontOfSize:30];

    titleLabel.textAlignment=NSTextAlignmentCenter;

    [self.viewaddSubview:titleLabel];


    

    UITableView * tableView=[[UITableViewalloc]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=[UIColorclearColor];

    [self.viewaddSubview: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=[tableViewdequeueReusableCellWithIdentifier:identy];

   if(!cell){

        cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:identy];

        

    }

    cell.backgroundColor=[UIColorclearColor];

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

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

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

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


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

    cell.imageView.image=[UIImageimageNamed:imageName];

   return cell;

    

}


//返回分组数量

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

    returnself.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=[tableViewdequeueReusableHeaderFooterViewWithIdentifier:identy];

   if(hf==nil){

        hf=[[UITableViewHeaderFooterViewalloc]initWithReuseIdentifier:identy];

       

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

        headView.tag=1111111;

        headView.backgroundColor=[UIColorcolorWithWhite:0.2 alpha:0.1];


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

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

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

        

        //添加三角形图标

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

//        imageView.userInteractionEnabled=YES;

        imageView.image=[UIImageimageNamed:@"disclosure"];

        imageView.tag=1001;

        

       //添加组名

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

        titleLabel.textAlignment=NSTextAlignmentCenter;

        titleLabel.tag=103;

        

        [headViewaddSubview:titleLabel];

        [headViewaddSubview:imageView];

        [headViewaddSubview:button];

        [hf.contentViewaddSubview:headView];


    }

    //重新获取headView

   UIView * headView=[hf viewWithTag:1111111];

    //获取button,设置标题

   UIButton * button=[headView.subviewslastObject];

    button.tag=section;

    //添加分组标题

   NSDictionary * temDic=self.dataArray[section];

   UILabel * temLabel=(UILabel *)[hfviewWithTag:103];

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

    

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

   UIImageView * imageView=(UIImageView *)[headViewviewWithTag:1001];

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


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

    [selfinitImageStateWithCondition: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.staticDicremoveObjectForKey:@(section)];

    }else{

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

    }

    

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

    NSIndexSet * set=[NSIndexSetindexSetWithIndex:section];

    [self.tableViewreloadSections:set withRowAnimation:UITableViewRowAnimationFade];

   

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

    [selfinitImageStateWithCondition: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