IOS实现仿qq好友列表功能

ios开发之实现仿qq好友列表功能


//
//  UserTableViewCell.h
//  UITableView协议
//
//  Created by mouweng on 17/8/26.
//  Copyright © 2017年 mouweng. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UserTableViewCell : UITableViewCell

@property (strong,nonatomic)UIImageView *headerPhoto;//头像
@property (strong,nonatomic)UILabel *nameLabel;//昵称
@property (strong,nonatomic)UILabel *isOnLine;//是否在线
@property (strong,nonatomic)UILabel *introductionLabel;//个性签名,动态
@property (strong,nonatomic)UILabel *networkLabel;//网络状态

@end


//
//  UserTableViewCell.m
//  UITableView协议
//
//  Created by mouweng on 17/8/26.
//  Copyright © 2017年 mouweng. All rights reserved.
//

#import "UserTableViewCell.h"

@implementation UserTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}


//重写初始化方法添加各个属性
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        _headerPhoto = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 50, 50)];
        [self.contentView addSubview:_headerPhoto];
        
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 5, 200, 25)];
        _nameLabel.backgroundColor = [UIColor clearColor];
        _nameLabel.font = [UIFont systemFontOfSize:16];
        [self.contentView addSubview:_nameLabel];
        
        _isOnLine = [[UILabel alloc] initWithFrame:CGRectMake(60, 40, 50, 5)];
        _isOnLine.font = [UIFont systemFontOfSize:10];
        [self.contentView addSubview:_isOnLine];
        
        _introductionLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 40, 180, 5)];
        _introductionLabel.font = [UIFont systemFontOfSize:10];
        [self.contentView addSubview:_isOnLine];
        
        _introductionLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 40, 180, 5)];
        _introductionLabel.font = [UIFont systemFontOfSize:10];
        [self.contentView addSubview:_introductionLabel];
        
        _networkLabel = [[UILabel alloc] initWithFrame:CGRectMake(320-50, 5, 50, 25)];
        _networkLabel.font = [UIFont systemFontOfSize:10];
        [self.contentView addSubview:_networkLabel];
        
        
        
    }
    return self;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end


//
//  ViewController.h
//  UITableView协议
//
//  Created by mouweng on 17/8/21.
//  Copyright © 2017年 mouweng. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
<
UITableViewDelegate,
UITableViewDataSource
>

{
    UITableView *_tableView;//定义数据视图对象
    
    NSMutableArray *selectedArr;//控制列表是否被打开

    NSArray *_titleArray;//第一层需要展示的数据
    NSDictionary *dataDic;//第二层需要展示的数据
    
    NSArray *friendsArray;//好友列表
    NSArray *familyArray;//亲戚列表
    NSArray *schoolmateArray;//同学列表
}

@end

//
//  ViewController.m
//  UITableView协议
//
//  Created by mouweng on 17/8/21.
//  Copyright © 2017年 mouweng. All rights reserved.
//

#import "ViewController.h"
#import "UserTableViewCell.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, 320, 500) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    
    [self initArrayDate];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 5, 320, 30)];
    titleLabel.text = [_titleArray objectAtIndex:section];
    [view addSubview:titleLabel];
    
    //添加图片
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(15 , 15, 10, 10)];
    imageView.tag = 20000+section;
    //根据当前是否展开设置图片
    NSString *string = [NSString stringWithFormat:@"%ld",section];
    if([selectedArr containsObject:string])
    {
        imageView.image = [UIImage imageNamed:@"arrow_down.jpg"];
    }
    else
    {
        imageView.image = [UIImage imageNamed:@"arrow_right.jpg"];
    }
    [view addSubview:imageView];
    
    //添加一个button 用来监听分组,实现分组的展开关闭
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 320, 50);
    btn.tag = 10000+section;
    [btn addTarget:self action:@selector(btnOpenList:) forControlEvents:UIControlEventTouchDown];
    [view addSubview:btn];
    
    return view;
}

- (void)btnOpenList:(UIButton *)sender
{
    NSString *string = [NSString stringWithFormat:@"%ld",sender.tag-10000];
    
    //数组selectedArr里面存的数据和表头想对应,方便以后做比较
    if ([selectedArr containsObject:string])
    {
        [selectedArr removeObject:string];
    }
    else
    {
        [selectedArr addObject:string];
    }
    
    [_tableView reloadData];
    
}




-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}



- (void)initArrayDate
{
    selectedArr = [[NSMutableArray alloc] init];
    _titleArray = [[NSArray alloc] initWithObjects:@"好友",@"亲戚",@"同学", nil];
    
    NSDictionary *dic01 = [[NSDictionary alloc]initWithObjectsAndKeys:@"张三丰",@"name",@"17_1.jpg",@"image" ,nil];
    NSDictionary *dic02 = [[NSDictionary alloc]initWithObjectsAndKeys:@"邓超",@"name", @"17_2.jpg",@"image" ,nil];
    NSDictionary *dic03 = [[NSDictionary alloc]initWithObjectsAndKeys:@"范冰冰",@"name", @"17_3.jpg",@"image" ,nil];
    friendsArray = [[NSArray alloc] initWithObjects:dic01,dic02,dic03 ,nil];
    
    NSDictionary *dic11 = [[NSDictionary alloc]initWithObjectsAndKeys:@"爸爸",@"name", @"17_4.jpg",@"image" ,nil];
    NSDictionary *dic12 = [[NSDictionary alloc]initWithObjectsAndKeys:@"妈妈",@"name",@"17_5.jpg",@"image" , nil];
    NSDictionary *dic13 = [[NSDictionary alloc]initWithObjectsAndKeys:@"姐姐",@"name",@"17_6.jpg",@"image" , nil];
    NSDictionary *dic14 = [[NSDictionary alloc]initWithObjectsAndKeys:@"弟弟",@"name",@"17_7.jpg",@"image" , nil];
    NSDictionary *dic15 = [[NSDictionary alloc]initWithObjectsAndKeys:@"哥哥",@"name", @"17_8.jpg",@"image" ,nil];
    familyArray = [[NSArray alloc] initWithObjects:dic11,dic12,dic13,dic14,dic15, nil];
    
    NSDictionary *dic21 = [[NSDictionary alloc]initWithObjectsAndKeys:@"胖虎",@"name",@"17_9.jpg",@"image" , nil];
    NSDictionary *dic22 = [[NSDictionary alloc]initWithObjectsAndKeys:@"大熊",@"name", @"17_10.jpg",@"image" ,nil];
    NSDictionary *dic23 = [[NSDictionary alloc]initWithObjectsAndKeys:@"小夫",@"name", @"17_11.jpg",@"image" ,nil];
    NSDictionary *dic24 = [[NSDictionary alloc]initWithObjectsAndKeys:@"静香",@"name",@"17_12.jpg",@"image" , nil];
    schoolmateArray = [[NSArray alloc] initWithObjects:dic21,dic22,dic23,dic24, nil];
    
    
    dataDic = [[NSDictionary alloc] initWithObjectsAndKeys:friendsArray,[_titleArray objectAtIndex:0],familyArray,[_titleArray objectAtIndex:1],schoolmateArray,[_titleArray objectAtIndex:2], nil];
}

//设置组数
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_titleArray count];
}

//设置每组里面的cell的个数
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[dataDic objectForKey:[_titleArray objectAtIndex:section]] count];
}


//给每一个cell定义以及设置内容
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *indexStr = [NSString stringWithFormat:@"%ld",indexPath.section];
    
    NSString *str = [_titleArray objectAtIndex:indexPath.section];
    NSArray *arr = [dataDic objectForKey:str];
    
    static NSString *CellIdentifier = @"MainCell";
    
    UserTableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:str];
    
    if(cell == nil)
    {
        cell = [[UserTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        //cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    
    if([selectedArr containsObject:indexStr])
    {
        NSDictionary *dic = [arr objectAtIndex:indexPath.row];
        cell.headerPhoto.image = [UIImage imageNamed:[dic valueForKey:@"image"]];
        cell.nameLabel.text = [dic valueForKey:@"name"];
        cell.isOnLine.text = @"[在线]";
        cell.introductionLabel.text = @"无动态";
        cell.networkLabel.text = @"4G";
    }
    
    return cell;
}







- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}



//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    return @"头部标题";
//}





//获取每一组的尾部标题
//- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
//{
//    return @"尾部标题";
//}

//设置头部和尾部的高度
//- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{
//    return 100;
//}
//
//- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 100;
//}





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

@end



更改ViewController 里面的设置cell个数的协议函数 可以实现更加cell随按钮的收缩而收缩

//设置每组里面的cell的个数
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //return [[dataDic objectForKey:[_titleArray objectAtIndex:section]] count];
    NSString *str = [NSString stringWithFormat:@"%ld",section];
    if([selectedArr containsObject:str])
    {
        return [[dataDic objectForKey:[_titleArray objectAtIndex:section]] count];
    }
    else{return 0;}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值