欢迎使用CSDN-markdown编辑器

iOS-UI-TableView-QQ列表Demo

首先这是工程的Demo
这里写图片描述
我把工程的Demo的展现给大家看,里面有一个plist文件,需要我们本地解析。再给大家写。
我们先来创建数据模型,用来解析这个文件。这里写图片描述
这里写图片描述

#import <UIKit/UIKit.h>
//添加一个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

做好了,就会出现如下效果这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值