仿qq界面的实现

来源:互联网 发布:beyond compare4.2mac 编辑:程序博客网 时间:2024/05/21 11:23


感谢qq好友的帮助,参考了部分博客。谢谢各位

//

//  TableViewController.h
//  qq列表界面
//
//  Created by jose on 15-3-9.
//  Copyright (c) 2015年 jose. All rights reserved.
//


#import <UIKit/UIKit.h>
//定义字典的关键字用于存储数值
#define DIC_EXPANDED @"expanded" //是否是展开 0收缩 1展开
#define DIC_ARARRY @"array"
#define DIC_TITILESTRING @"title"
#define heigt 40.0f
@interface TableViewController : UITableViewController<UITextFieldDelegate>
{
    //声明变量
    NSMutableArray *data;
    //下拉刷新
    UIRefreshControl *refresh;
    UISearchBar *search;
}


@property(nonatomic,strong)UIRefreshControl *refresh;
@property(nonatomic,strong)UISearchBar *search;

@end


*****************************************************************************************************************

****************************************************************************************************************

****************************************************************************************************************

//
//  TableViewController.m
//  qq列表界面
//
//  Created by jose on 15-3-9.
//  Copyright (c) 2015年 jose. All rights reserved.
//


#import "TableViewController.h"


@interface TableViewController ()


@end


UIButton *mybutton;
CGFloat width;
UIView *headview;
@implementation TableViewController
@synthesize refresh;
@synthesize search;


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initdata];
    [self setbeginrefresh];
    //取出tableview的底部多余的表格
    self.tableView.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];
     headview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, width, 160)];
    headview.backgroundColor=[UIColor whiteColor];
    self.tableView.tableHeaderView=headview;
    width=[UIScreen mainScreen].bounds.size.width;
    [self button];
    [self setsearch];
    
    //添加手势
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureclick)];
    [self.view addGestureRecognizer:gesture];
       }
//相应手势的事件


-(void)gestureclick{
     [self.view endEditing:YES];
}


//创建按钮
-(void)button{
    for (int i=0; i<4; i++) {
        UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(i*(width/4)+10,80, 60, 60)];
        [button setTitle:@"点你" forState:UIControlStateNormal];
        button.backgroundColor=[UIColor grayColor];
        button.layer.cornerRadius=8.0f;
        button.layer.masksToBounds=YES;
        [headview addSubview:button];
    }
    
    UILabel *labeltop=[[UILabel alloc]initWithFrame:CGRectMake(0, 77, width, 1)];
    labeltop.backgroundColor=[UIColor blueColor];
    
    UILabel *labelbottom=[[UILabel alloc]initWithFrame:CGRectMake(0,142, width, 1)];
    labelbottom.backgroundColor=[UIColor blueColor];
    
    [headview addSubview:labeltop];
    [headview addSubview:labelbottom];
}


//设置搜索条
-(void)setsearch{
    search=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 20, width, 40)];
    search.backgroundColor=[UIColor whiteColor];
    [headview addSubview:search];
}


//设置刷新
-(void)setbeginrefresh{
    refresh=[[UIRefreshControl alloc]init];
    refresh.tintColor=[UIColor lightGrayColor];
    refresh.attributedTitle=[[NSAttributedString alloc]initWithString:@"下拉刷新"];
    [refresh addTarget:self action:@selector(refreshaction:) forControlEvents:UIControlEventValueChanged];
    self.refreshControl=refresh;
}


//触发刷新
-(void)refreshaction:(UIRefreshControl *)refreshs{
    if(refreshs.refreshing)
    {
        refresh.attributedTitle=[[NSAttributedString alloc]initWithString:@"正在刷新"];
        [self performSelector:@selector(refreshdata) withObject:nil afterDelay:2];
    }
}


//刷新数据
-(void)refreshdata{
    //NSString *systemtime=nil;
    //设置时间格式
    NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    //systemtime=[formatter stringFromDate:[NSDate date]];
    NSString *lasttime=[NSString stringWithFormat:@"上一次更新时间为%@",[formatter stringFromDate:[NSDate date]]];
    self.refreshControl.attributedTitle=[[NSAttributedString alloc]initWithString:lasttime];
    [self.refreshControl endRefreshing];
    [self.tableView reloadData];
    
}




-(void)initdata{
    
    //初始化数组,用于存储数据
    data=[[NSMutableArray alloc]init];
    
    for (int i=0; i<=5; i++) {
        
        NSMutableArray *array=[[NSMutableArray alloc]init];
        for (int j=0; j<=5; j++) {
            NSString *stringone=[NSString stringWithFormat:@"%i组%i行",i,j];
            [array addObject:stringone];
        }
        NSString *string=[NSString stringWithFormat:@"第%i分组",i];
        
        NSMutableDictionary *dic=[[NSMutableDictionary alloc]initWithObjectsAndKeys:array,DIC_ARARRY,string,DIC_TITILESTRING,[NSNumber numberWithInt:0],DIC_EXPANDED, nil];
        
        [data addObject:dic];
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}






//按钮点击事件
-(void)buttonclick:(UIButton *)sender{
    NSInteger section=sender.tag;
    [self collapseorexpand:section];
    [self.tableView reloadData];
}


//判断是否展开或者收缩
-(void)collapseorexpand:(NSInteger)section{
    NSMutableDictionary *dic=[data objectAtIndex:section];
    NSInteger expanded=[[dic objectForKey:DIC_EXPANDED]intValue];
    if(expanded)
    {
        [dic setValue:[NSNumber numberWithInt:0] forKey:DIC_EXPANDED];
    }
    else
    {
        [dic setValue:[NSNumber numberWithInt:1] forKey:DIC_EXPANDED];
    }
}


#pragma mark - Table view data source
//返回有多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return data.count;
}
//每组有多少条数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSMutableDictionary *dic=[data objectAtIndex:section];
    NSArray *array=[dic objectForKey:DIC_ARARRY];
    if([[dic objectForKey:DIC_EXPANDED]intValue])
    {
        return array.count;
    }
    else
        return 0;
    
 }


//显示每组数据的单元格
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //定义个静态字符串为了防止与其他类的tableivew重复
    
    static NSString *table=@"cell";
    
    //定义cell的复用性当处理大量数据时减少内存开销
    
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:table];
    
    if(cell==nil)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:table];
    }
    NSArray *array=[[data objectAtIndex:indexPath.section]objectForKey:DIC_ARARRY];
    cell.textLabel.text=[array objectAtIndex:indexPath.row];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    self.tableView.separatorColor=[UIColor blueColor];
    return cell;
}


//设置分组头的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return heigt;
}




//设置分组头
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    //添加自定义视图
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0,width, 40)]
    ;
    view.backgroundColor=[UIColor whiteColor];
    //添加按钮
    mybutton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, width, 40)];
    mybutton.tag=section;
    mybutton.backgroundColor=[UIColor whiteColor];
    [mybutton addTarget:self action:@selector(buttonclick:) forControlEvents:UIControlEventTouchUpInside];
    //设置标题
    [mybutton setTitle:[[data objectAtIndex:section] objectForKey:DIC_TITILESTRING] forState:UIControlStateNormal];
    [mybutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [view addSubview:mybutton];
    //添加上划线
    UILabel *labeltop=[[UILabel alloc]initWithFrame:CGRectMake(0,-1, width, 1)];
    labeltop.backgroundColor=[UIColor blueColor];
    [view addSubview:labeltop];
    //添加下划线
    UILabel *labelbottom=[[UILabel alloc]initWithFrame:CGRectMake(0, 39, width, 1)];
    labelbottom.backgroundColor=[UIColor blueColor];
    [view addSubview:labelbottom];
    return view;
}
//单元内容递进
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 1;
}


@end



0 0
原创粉丝点击