A.需求
1.使用plist数据,展示类似QQ好友列表的分组、组内成员显示缩进功能
2.组名使用Header,展示箭头图标、组名、组内人数和上线人数
3.点击组名,伸展、缩回好友组
 
code source: https://github.com/hellovoidworld/QQFriendList
 
B.实现步骤
1.编写MVC结构
 
 
(1)根据plist文件结构,编写model,使用嵌套型
 
 
 //
// FriendGroup.h
// FriendsList
//
// Created by hellovoidworld on 14/12/12.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import <Foundation/Foundation.h> @class Friend; @interface FriendGroup : NSObject /** 好友组 */
@property(nonatomic, strong) NSArray *friends; /** 好友组名 */
@property(nonatomic, copy) NSString *name; /** 在线人数 */
@property(nonatomic, assign) int online; - (instancetype) initWithDictionary:(NSDictionary *) dictionary;
+ (instancetype) friendGroupWithDictionary:(NSDictionary *) dictionary; @end
 
(2)自定义header,用于组名显示,包括了箭头图示、组名、在线人数/组内人数
 
a.数据来源:FriendGroup模型
 /** 加载数据  */
- (void)setFriendGroup:(FriendGroup *)friendGroup
 
b.在init方法中header控件的位置尺寸信息并没有初始化,init方法仅仅用于分配内存,所以要使用初始化后的header位置尺寸数据,要在layoutSubviews方法中使用
 /** 子控件布局方法
在init的时候,只分配的内存,没有初始化控件的尺寸,在此处header已经有了位置尺寸了
*/
- (void)layoutSubviews {
// 必须先调用父类的方法
[super layoutSubviews]; // 1.背景
self.headerButtonView.frame = self.bounds; // 2.在线人数/组内总人数
CGFloat countWidth = ;
CGFloat countHeight = self.frame.size.height;
CGFloat countX = self.frame.size.width - - countWidth;
CGFloat countY = ;
self.onlineCountView.frame = CGRectMake(countX, countY, countWidth, countHeight);
}
 
c.使用tableView的缓存池,在controller中实现相应dataSource方法
 /** 自定义每个section的头部 */
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
FriendHeader *header = [FriendHeader friendHeaderWithTableView:self.tableView];
header.friendGroup = self.friendGroups[section];
return header;
}
 
(3)定义cell,用于显示每个好友信息,包括头像、昵称、介绍(签名)
 
实现方法跟header差不多
数据来源:Friend模型
在这里cell不用计算子控件的位置尺寸,直接使用 UITableViewCellStyleSubtitle类型的cell
 /** 自定义构造方法 */
+ (instancetype) cellWithTableView:(UITableView *) tableView {
static NSString *ID = @"friendCell";
FriendCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (nil == cell) {
cell = [[self alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
} return cell;
} /** 加载数据 */
- (void)setFriendData:(Friend *)friendData {
_friendData = friendData; self.imageView.image = [UIImage imageNamed:friendData.icon];
self.textLabel.text = friendData.name;
self.textLabel.textColor = friendData.isVip?[UIColor redColor]:[UIColor blackColor];
self.detailTextLabel.text = friendData.intro;
}
 
初步可以显示整个版面:
 
2.伸缩组内好友
(1)在group模型创建一个标识,用来标识此组的好友是否显示
 /** 是否伸展显示好友 */
@property(nonatomic, assign, getter=isOpened) BOOL opened;
 
(2)给header加上点击事件,改变伸展标识
         // 1.4点击事件
[headerButtonView addTarget:self action:@selector(headerClicked) forControlEvents:UIControlEventTouchUpInside];
 
 /** 点击事件 */
- (void) headerClicked {
// 1.伸展、隐藏组内好友
self.friendGroup.opened = !self.friendGroup.isOpened; // 2.刷新tableView
if ([self.delegate respondsToSelector:@selector(friendHeaderDidClickedHeader:)]) {
[self.delegate friendHeaderDidClickedHeader:self];
}
}
 
(3)给header编写代理协议
 @protocol FriendHeaderDelegate <NSObject>
/** header被点击的代理方法 */
@optional
- (void) friendHeaderDidClickedHeader:(FriendHeader *) header; @end
 
(4)viewController根据group模型的伸展标识,判断是否返回当前组的row数
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
FriendGroup *group = self.friendGroups[section];
// 先检查模型数据内的伸展标识
return group.isOpened? group.friends.count : ;
}
 
(5)使用viewController作为代理,监听header的点击事件,刷新tableView数据
 #pragma mark - FriendHeaderDelegate方法
- (void)friendHeaderDidClickedHeader:(FriendHeader *)header {
// 刷新数据
[self.tableView reloadData];
}
 
 
3.改变组的伸展标识—箭头朝向
(1)由于在header的代理中使用了tableView的 reloadData刷新界面和数据,tableView会从新创建所有header,所以在reloadData之前改变header的外观(比如箭头朝向)是没有意义的。
     所以需要修改新创建的header
 /**
被加到父控件之前
由于tableView刷新数据后,所有header会被重新创建,所以要在这里对箭头朝向做出修改
*/
- (void)didMoveToSuperview {
// 改变箭头朝向,顺时针旋转90度
CGFloat rotation = self.friendGroup.isOpened? M_PI_2 : ;
self.headerButtonView.imageView.transform = CGAffineTransformMakeRotation(rotation);
}
 
(2)使用transform旋转箭头,因为原来的图片大小非正方形而且填充方式是拉伸,需要做一些属性修改
         // 改变箭头填充方式为居中
[headerButtonView.imageView setContentMode:UIViewContentModeCenter];
// 不需要裁剪箭头图片的边界
[headerButtonView.imageView setClipsToBounds:NO];
 
 
 

[iOS基础控件 - 6.9.3] QQ好友列表Demo TableView的更多相关文章

  1. [iOS基础控件 - 6.12.1] QQ菜单管理 UITabBarController 控制器管理

    A.需求 1.类似QQ.微信顶部或者底部的窗口转换导航条 2.给每个页面添加相应内容   B.UITabBarController 1.基本概念: (1)内容高度 iOS7之前内容高度为:屏幕高度 - ...

  2. [iOS基础控件 - 4.4] 进一步封装"APP列表”,初见MVC模式

    A.从ViewController分离View 之前的代码中,View的数据加载逻辑放在了总的ViewController中,增加了耦合性,应该对控制器ViewController隐藏数据加载到Vie ...

  3. [iOS基础控件 - 6.10.1] PickerView 餐点搭配Demo

    A.需求 1.使用PickerView做出有3列餐点(水果.主菜.饮料)的搭配Demo 2.选择的餐点实时显示在“显示区” 3.提供“随机”按钮,随机选择菜品搭配   B.实现步骤 1.拖入一个Pic ...

  4. [iOS基础控件 - 5.5] 代理设计模式 (基于”APP列表"练习)

    A.概述      在"[iOS基础控件 - 4.4] APP列表 进一步封装,初见MVC模式”上进一步改进,给“下载”按钮加上效果.功能      1.按钮点击后,显示为“已下载”,并且不 ...

  5. [iOS基础控件 - 3.1] QQ登陆界面

      A.storyboard 控件版 1.label 2.textfield      a.Keyboard Type           账号:Number Pad           密码:Num ...

  6. iOS 基础控件(下)

    上篇介绍了UIButton.UILabel.UIImageView和UITextField,这篇就简短一点介绍UIScrollView和UIAlertView. UIScrollView 顾名思义也知 ...

  7. [iOS基础控件 - 6.9] 聊天界面Demo

    A.需求 做出一个类似于QQ.微信的聊天界面 1.每个cell包含发送时间.发送人(头像).发送信息 2.使用对方头像放在左边,我方头像在右边 3.对方信息使用白色背景对话框,我方信息使用蓝色背景对话 ...

  8. [iOS基础控件 - 7.0] UIWebView

    A.基本使用 1.概念 iOS内置的浏览器控件 Safari浏览器就是通过UIWebView实现的   2.用途:制作简易浏览器 (1)基本请求 创建请求 加载请求 (2)代理监听webView加载, ...

  9. [iOS基础控件 - 6.11.3] 私人通讯录Demo 控制器的数据传递、存储

    A.需求 1.搭建一个"私人通讯录"Demo 2.模拟登陆界面 账号 密码 记住密码开关 自动登陆开关 登陆按钮 3.退出注销 4.增删改查 5.恢复数据(取消修改)   这个代码 ...

随机推荐

  1. JUnit4概述

    JUnit4是JUnit框架有史以来的最大改进,其主要目标便是利用Java5的Annotation特性简化测试用例的编写. 先简单解释一下什么是Annotation,这个单词一般是翻译成元数据.元数据 ...

  2. 1085: [SCOI2005]骑士精神

    A*搜索. A*搜索的基础百度百科(实在偷懒没看论文之类的),在这里不说了. A*搜索很关键的是h(n)估价函数的选取(表示现在到结束节点需要的距离) 设d(n)为实际到结束节点的距离.h(n)< ...

  3. ionic cordova plugin for ios

    源代码结构目录: payplugin: |_src |_android |_PayPlugin.java |_ios |_CDVPayPlugin.h |_CDVPayPlugin.m |_www | ...

  4. UVA 550 Multiplying by Rotation (简单递推)

    题意:有些数字是可以这样的:abcd*k=dabc,例如179487 * 4 = 717948,仅仅将尾数7移动到前面,其他都不用改变位置及大小.这里会给出3个数字b.d.k,分别代表b进制.尾数.第 ...

  5. noip2005提高组题解

    05年的题目绝对是自2000年以来难度最大的.后三题的难度系数分别为0.2.0.2.0.3,而前面几年的题目中每年最多只出现一道难度系数为0.2的题目,其难度可见一斑. 强烈推荐这个 PPT,每道题都 ...

  6. 在VC中显示和处理图片的方法

    落鹤生 发布于 2011-10-21 09:12 点击:344次  来自:blog.csdn.net/mengaim_cn 几种用GDI画图的方法介绍. TAG: GDI   法1:这个方法其实用的是 ...

  7. poj 3352 Road Construction

    // 只能说这题和上题一模一样// 我就直接贴上题代码了.. #include <iostream> #include <algorithm> #include <que ...

  8. eclipse设置自定义快捷键

    eclipse有很多强大且人性化的功能,而各项功能有时又隐藏得比较深(需要点击数次菜单才能找到),而系统提供的快捷键有时比较难记住甚至根本没有提供快捷键时,就需要自己手动设置快捷键了.设置方法有两种, ...

  9. Js原型模式

    function Person(){ } Person.prototype.name = "xd"; Person.prototype.age = 26; Person.proto ...

  10. redis学习笔记之pipeline

    redis是一个cs模式的tcp server,使用和http类似的请求响应协议.一个client可以通过一个socket连接发起多个请求命令.每个请求命令发出后client通常 会阻塞并等待redi ...