欢迎使用CSDN-markdown编辑器

iOS-UI-TableView-QQ列表Demo

首先这是工程的Demo
欢迎使用CSDN-markdown编辑器_第1张图片
我把工程的Demo的展现给大家看,里面有一个plist文件,需要我们本地解析。再给大家写。
我们先来创建数据模型,用来解析这个文件。这里写图片描述
欢迎使用CSDN-markdown编辑器_第2张图片

#import 
//添加一个Friend类,添加数据模型
@interface Friend : NSObject

@property (strong,nonatomic) NSString *icon;
@property (strong,nonatomic) NSString *intro;
@property (strong,nonatomic) NSString *name;
@property (assign,nonatomic) NSInteger vip;
//对数据进行初始化
- (id)initWithDic:(id)dic;

@end

@interface GroupModel : NSObject

@property (strong,nonatomic) NSMutableArray *friends;
@property (assign,nonatomic) NSInteger online;
@property (strong,nonatomic) NSString *name;
@property (assign,nonatomic) NSInteger isShow;
//给它一个方法,给模型进行初始化
- (id)initWithDic:(id)dic;

@end
我们来看看.m文件的实现

//
//  GroupModel.m
//  UI-作业-10
//
//  Created by on 16/1/11.
//  Copyright (c) 2016年 LS. All rights reserved.
//

#import "GroupModel.h"

@implementation Friend

- (id)initWithDic:(id)dic
{
    if (self = [super init])
    {
        self.icon = dic[@"icon"];
        self.intro = dic[@"intro"];
        self.name = dic[@"name"];
        self.vip = [dic[@"vip"]integerValue];
    }
        return self;
}

@end

@implementation GroupModel

- (id)initWithDic:(id)dic
{
    if (self = [super init])
    {
        _friends = [[NSMutableArray alloc]init];
        _name = dic[@"name"];
        _online = [dic[@"online"]integerValue];
        _isShow = NO;
    for (id obj in dic[@"friends"])
    {
        Friend *friend = [[Friend alloc]initWithDic:obj];
        [_friends addObject:friend];

    }

    }
        return self;
}
@end
上面是对plist文件的解析,为后面列表信息做准备
//
//  ViewController.m
//  UI-作业-10
//
//  Created by wanlinsheng on 16/1/10.
//  Copyright (c) 2016年 LS. All rights reserved.
//

#import "ViewController.h"
#import "GroupModel.h"
#define kCellID @"cellID"
#define kHeaderID @"headerID"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

@end

@implementation ViewController
{
    UITableView *_tableView;

    NSMutableArray *_groupList;

}


- (void)initGroupList
{
    _groupList = [[NSMutableArray alloc]init];

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

    NSArray *group = [NSArray arrayWithContentsOfFile:path];


    for (id obj in group)
    {
        GroupModel *group = [[GroupModel alloc]initWithDic:obj];
        [_groupList addObject:group];
      //  NSLog(@"%@",_groupList);
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];

    // 实例化
    [self initGroupList];
    [self initTableView];

}

- (void)initTableView
{

    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];

    _tableView.delegate = self;

    _tableView.dataSource = self;


    [self.view addSubview:_tableView];

    _tableView.tableFooterView = [UIView new];


}

#pragma mark - UITableViewDataSource/Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _groupList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    GroupModel *group = _groupList[section];

    if (group.isShow)
    {

    return group.friends.count;
    }
    return  0;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (!cell)
    {

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

    }
    GroupModel *group = _groupList[indexPath.section];


    Friend *friend = group.friends[indexPath.row];

    cell.imageView.image = [UIImage imageNamed:friend.icon];

    cell.textLabel.text = friend.name;

    cell.detailTextLabel.text = friend.intro;

    return cell;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *headerID = @"headerID";
    UITableViewHeaderFooterView *header = [tableView dequeueReusableCellWithIdentifier:headerID];

    if (!header)
    {
        header = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:headerID];

        UIImageView *arrowIcon = [[UIImageView alloc]initWithFrame:CGRectMake(15,12, 15, 15)];
        arrowIcon.tag = 100;
        [header addSubview:arrowIcon];

        UILabel *titleLbl = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(arrowIcon.frame)+10, 0, 200, 45)];
        titleLbl.textColor = [UIColor grayColor];
        titleLbl.tag = 200;
        [header addSubview:titleLbl];

        UILabel *online = [[UILabel alloc]initWithFrame:CGRectMake(tableView.bounds.size.width-100, 0, 90, 45)];
        online.textColor = [UIColor grayColor];
        online.font = [UIFont systemFontOfSize:15];
        online.textAlignment = NSTextAlignmentRight;
        online.tag = 300;
        [header addSubview:online];


        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headerTapHandle:)];
        [header.contentView addGestureRecognizer:tap];

    }

        UIImageView *arrorIcon =(UIImageView*) [header viewWithTag:100];
        UILabel *titleLbl = (UILabel*)[header viewWithTag:200];
        UILabel *onlineLbl = (UILabel *)[header viewWithTag:300];

        header.contentView.tag = 500 + section;

        //获取对应的数组
        GroupModel *group = _groupList[section];

        arrorIcon.image = group.isShow ? [UIImage imageNamed:@"header_arrow_down"]:
        [UIImage imageNamed:@"header_arrow_right"];
        titleLbl.text = group.name;
        onlineLbl.text = [NSString stringWithFormat:@"%ld/%ld",group.online,group.friends.count];


        return header;


}

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

#pragma mark - 组头点击的事件

- (void)headerTapHandle:(UITapGestureRecognizer *)tap
{

    GroupModel *group = _groupList[tap.view.tag - 500];

    group.isShow = !group.isShow;

    NSIndexSet *set = [NSIndexSet indexSetWithIndex:tap.view.tag - 500];
    [_tableView reloadSections:set withRowAnimation:UITableViewRowAnimationAutomatic];
}

@end

做好了,就会出现如下效果欢迎使用CSDN-markdown编辑器_第3张图片

你可能感兴趣的:(ios学习,ios-UI)