UI基础动画相关iOS开发好文

iOS实战项目开发:QQ列表展示

2015-07-30  本文已影响1071人  952625a28d0d

头像:


Paste_Image.png

#import <Foundation/Foundation.h>

@interface JFriendsModel : NSObject

@property(nonatomic, copy) NSString * icon;   // 头像
@property(nonatomic, copy) NSString * name; // 姓名
@property(nonatomic, copy) NSString * intro; // 签名
@property(nonatomic, assign) BOOL  isVip; // vip

- (instancetype)initWithDict:(NSDictionary *)dict; // 初始化方法
+ (instancetype)friendWithDict:(NSDictionary *)dict; // 遍历构造器

@end```

- 方法实现

import "JFriendsModel.h"

@implementation JFriendsModel

@end```

#import <Foundation/Foundation.h>

@class JFriendsModel;
@interface JGroupModel : NSObject

@property (nonatomic, copy) NSString *name; // 分组名称
@property (nonatomic, copy) NSString *online;   // 在线人数
@property (nonatomic, strong) NSArray *friends; // 好友数组
@property(nonatomic, strong) JFriendsModel * friendModel;   // 好友Model
@property(nonatomic, assign) BOOL  isOpen;  // 点击展开或者关闭状态

- (instancetype)initWithDict:(NSDictionary *)dict;  // 初始化方法
+ (instancetype)groupWithDict:(NSDictionary *)dict; // 遍历构造器

@end```

- 方法实现

import "JGroupModel.h"

import "JFriendsModel.h"

@implementation JGroupModel

/**

/**

@end```

#import <UIKit/UIKit.h>
#import "JGroupModel.h"

/**
 *  头视图的代理方法
 */
@protocol HeaderViewDelegate <NSObject>

@optional

- (void)clickView;  // 点击头视图事件

@end

@interface HeaderView : UITableViewHeaderFooterView

@property (nonatomic, assign) id<HeaderViewDelegate> delegate;  // 代理
@property (nonatomic, strong) JGroupModel *groupModel;  // 头视图数据模型

+ (instancetype)headerView:(UITableView *)tableView;    // 初始化方法

@end```

- 方法实现

import "HeaderView.h"

import "JGroupModel.h"

@implementation HeaderView{
UIButton *_arrowButton; // 点击按钮
UILabel *_label; // 标题
}

pragma mark - buttonAction

pragma mark - 调用系统方法对button的箭头图片实现旋转

pragma mark - 系统方法设置Frame值

pragma mark - 属性set方法给控件赋值

#import "ListTableViewController.h"
#import "JGroupModel.h"
#import "JFriendsModel.h"
#import "HeaderView.h"
#import "ViewController.h"

@interface ListTableViewController ()<HeaderViewDelegate>   // 遵守代理

@property (nonatomic, strong) NSArray *dataArray;   // 数据数组

@end

@implementation ListTableViewController

// 懒加载
- (NSArray *)dataArray
{
    if (!_dataArray) {
        // 获取数据
        NSString *path = [[NSBundle mainBundle] pathForResource:@"friends.plist" ofType:nil];
        NSArray *array = [NSArray arrayWithContentsOfFile:path];
        NSLog(@"%@", array);
        // 初始化可变数组,数量为获取到的数组的数量
        NSMutableArray *muArray = [NSMutableArray arrayWithCapacity:array.count];
        // 遍历
        for (NSDictionary *dict in array) {
            // 初始化分组模型
            JGroupModel *md = [JGroupModel groupWithDict:dict];
            // 添加至可变数组
            [muArray addObject:md];
        }
        // 赋值给本控制器数据数组
        _dataArray = [muArray copy];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 重设tableHeaderView的高度
    self.tableView.sectionHeaderHeight = 40;
    // 取出tableView多余的分割线
    [self clickExtraLine:self.tableView];
}

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

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    JGroupModel *md = self.dataArray[section];
    // 判断是展开还是关闭,如果是展开则返回friend的个数,如果是关闭则返回0
    NSInteger count = md.isOpen ? md.friends.count : 0;
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"friendCell";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
// 以上不能用forIndexPath:indexPath这个方法,不然会出现重用的崩溃
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    JGroupModel *md = self.dataArray[indexPath.section];    // 现获取分组模型
    JFriendsModel *friendMd = md.friends[indexPath.row];    // 获取row模型
    // 赋值
    cell.imageView.image = [UIImage imageNamed:friendMd.icon];
    cell.textLabel.text = friendMd.name;
    cell.detailTextLabel.text = friendMd.intro;
    return cell;
}

// 点击进入详情
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ViewController *vc = [[ViewController alloc] init];
    vc.view.backgroundColor = [UIColor redColor];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UITableView Delegate 
// 添加头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HeaderView *headView = [HeaderView headerView:tableView];
    headView.delegate = self;
    headView.groupModel = self.dataArray[section];  // 给头视图传递数据模型
    return headView;
}

#pragma mark - 代理方法的实现
- (void)clickView
{
    [self.tableView reloadData];
}

#pragma mark - 去掉多余的线
- (void)clickExtraLine:(UITableView *)tableView
{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    [self.tableView setTableFooterView:view];
}

@end```

- 运行效果:

![QQList.gif](http://upload-images.jianshu.io/upload_images/189984-c5d1acad818a88f5.gif?imageView2/2/w/1240)
上一篇 下一篇

猜你喜欢

热点阅读