QQ好友列表

#import "ListController.h"
#import "ViewController.h"

#import "HeaderView.h"
#import "FriendModel.h"
#import "Friend.h"

@interface ListController ()<HeaderViewDelegate>

@property (nonatomic, strong) NSMutableArray *friendsData;

@end

@implementation ListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
      self.automaticallyAdjustsScrollViewInsets = YES;
    
    self.title = @"好友列表";
    self.view.backgroundColor = [UIColor yellowColor];
    
    self.tableView.sectionHeaderHeight = 40;//分区高度
    
    [self loadData];//读取数据
    
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

//读取数据
- (void)loadData {
    //获取路径
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"friends" withExtension:@"plist"];
    NSArray *tempAry = [NSArray arrayWithContentsOfURL:url];
    NSMutableArray *ary = [NSMutableArray array];
    for (NSDictionary *dic in tempAry) {
        FriendModel *model = [FriendModel friendModelWithDic:dic];
        [ary addObject:model];
    }
    self.friendsData = [NSMutableArray array];
    _friendsData = ary;
    NSLog(@"!!!!%@",_friendsData);
}

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

#pragma mark - Table view data source
//分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _friendsData.count;
}
//分区行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    FriendModel *model = _friendsData[section];
    NSLog(@"+++%lu",(unsigned long)model.friends.count);
    NSInteger count = model.isOpened ? model.friends.count : 0;
    return count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    FriendModel *friendModel = _friendsData[indexPath.section];
    Friend *friend = friendModel.friends[indexPath.row];
    
    cell.imageView.image = [UIImage imageNamed:friend.icon];
    cell.textLabel.text = friend.name;
    cell.detailTextLabel.text = friend.intro;
        
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ViewController *viewVC = [[ViewController alloc] init];
    [self.navigationController pushViewController:viewVC animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    HeaderView *headerView =[HeaderView headViewWithTableView:tableView];
    headerView.delegate = self;
    headerView.friendModel = _friendsData[section];
    headerView.contentView.backgroundColor = [UIColor orangeColor];
    return headerView;
}

- (void)clickHeadView {
    [self.tableView reloadData];
    
}
@end

import "FriendModel.h"
#import "Friend.h"

@implementation FriendModel

- (id)initWithDic:(NSDictionary *)dic {
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dic];
        NSMutableArray *tempAry = [NSMutableArray array];
        for (NSDictionary *dic1 in _friends) {
            Friend *friend = [Friend friendWithDic:dic1];
            [tempAry addObject:friend];
        }
        _friends = tempAry;
    }
    return self;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {

}
+ (id)friendModelWithDic:(NSDictionary *)dic {
    return [[self alloc] initWithDic:dic];
}

@end

#import <Foundation/Foundation.h>

@interface Friend : NSObject

@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *intro;
@property (nonatomic, copy) NSString *name;

- (id)initWithDic:(NSDictionary *)dic;

+ (id)friendWithDic:(NSDictionary *)dic;

@end

#import <UIKit/UIKit.h>

@class FriendModel;

@protocol HeaderViewDelegate <NSObject>

- (void)clickHeadView;

@end



@interface HeaderView : UITableViewHeaderFooterView

@property (nonatomic, strong) FriendModel *friendModel;

@property (nonatomic, assign) id <HeaderViewDelegate>delegate;

+ (id)headViewWithTableView:(UITableView *)tableView;

@end

#import "HeaderView.h"
#import "FriendModel.h"

@interface HeaderView ()

@property (nonatomic, strong) UIButton *bgButton;
@property (nonatomic, strong) UILabel *numLabel;

@end

@implementation HeaderView

+ (id)headViewWithTableView:(UITableView *)tableView {
    static NSString *identifier = @"headView";
    HeaderView *headView = (HeaderView *)[tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (headView == nil) {
        headView = [[HeaderView alloc] initWithReuseIdentifier:identifier];
    }
    return headView;
}

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        self.bgButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_bgButton setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg"] forState:(UIControlStateNormal)];
         [_bgButton setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg_highlighted"] forState:(UIControlStateHighlighted)];
        
        [_bgButton setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:(UIControlStateNormal)];
        [_bgButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        
        _bgButton.imageView.contentMode = UIViewContentModeCenter;
        _bgButton.imageView.clipsToBounds = NO;
        _bgButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//位置
        _bgButton.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        _bgButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        [_bgButton addTarget:self action:@selector(headBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
        [self addSubview:_bgButton];
        
        self.numLabel = [[UILabel alloc] init];
        _numLabel.textAlignment = NSTextAlignmentRight;
        [self addSubview:_numLabel];
    }
    return self;
}

- (void)headBtnClick {
    _friendModel.opened = !_friendModel.isOpened;

    if ([_delegate respondsToSelector:@selector(clickHeadView)]) {
        [_delegate clickHeadView];
    }
}

- (void)setFriendModel:(FriendModel *)friendModel {
    _friendModel = friendModel;
    [_bgButton setTitle:friendModel.name forState:(UIControlStateNormal)];
    _numLabel.text = [NSString stringWithFormat:@"%lu",(unsigned long)friendModel.friends.count];
}

- (void)didMoveToSuperview {
    _bgButton.imageView.transform = _friendModel.isOpened ? CGAffineTransformMakeRotation(M_PI_2) : CGAffineTransformMakeRotation(0);
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _bgButton.frame = self.bounds;
    _numLabel.frame = CGRectMake(self.frame.size.width - 70, 0, 60, self.frame.size.height);
}

@end


你可能感兴趣的:(QQ列表折叠功能)