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. JAVA中获取工程路径的方法

    在jsp和class文件中调用的相对路径不同.在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getProp ...

  2. mssql修改链接数为默认值

    EXEC sys.sp_configure N'show advanced options', N'1'  RECONFIGURE WITH OVERRIDE GO EXEC sys.sp_confi ...

  3. UVa 10285 Longest Run on a Snowboard【记忆化搜索】

    题意:和最长滑雪路径一样, #include<iostream> #include<cstdio> #include<cstring> #include <c ...

  4. Android listView如何判断数据多屏显示 (2012-03-06 09:47:04)

    @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int tot ...

  5. android开发中eclipse里xml开发的自动提示和使用帮助快捷键提示

    Eclipse Android 代码自动提示功能 Eclipse for android 设置代码提示功能 打 开 Eclipse 依次选择 Window > Preferences > ...

  6. 编译pure-ftpd时提示错误Your MySQL client libraries aren't properly installed

    如果出现类似configure: error: Your MySQL client libraries aren’t properly installed 的错误,请将mysql目录下的 includ ...

  7. ActionBarSherlock的学习笔记(四) ------------ ActionBarSherlock中的搜索及SearchView的使用

    在使用ActionBarSherlock定义app的头部操作时,会经常看见搜索的动作,本文主要介绍一下搜索是如何实现的. 1. SearchView 是搜索的核心组件,具体介绍请参考Android官方 ...

  8. 一天一个Java基础——对象和类

    1.在Java中你所做的全部工作就是定义类,产生那些类的对象,以及发送消息给这些对象 2.可以在类中设置两种类型的元素:字段(也被称作数据成员)和方法(也被称作成员函数) 3.字段可以是任何类型的对象 ...

  9. java 访问器方法中对象引用的问题

    "注意不要编写返回引用可变对象的访问器方法".因为会破坏类的封装性,引用的内容可能会被改变,产生业务逻辑上的错误. 什么是可变对象? 先要搞清楚java中值传递和引用传递的问题,总结如下: 1.对象就 ...

  10. AFNetworking速成教程

    转:http://palmsky.net/?p=4138 本文是由 iOS Tutorial 小组成员 Scott Sherwood撰写,他是一个基于位置动态加载(Dynamically Loaded ...