swift 实现QQ好友列表功能
最近项目中有类似QQ好友列表功能,整理了一下,话不多说,直接上代码
import UIKit
class QQFriend: NSObject {
var name: String?
var intro: String?
init(dic: NSDictionary) {
super.init()
self.setValuesForKeys(dic as! [String : AnyObject])
}
}
import UIKit
class QQFriendGroup: NSObject {
var name: String?
var friends: NSArray?
var isOpen: Bool? = false
init(withDic dic: NSDictionary) {
super.init()
self.setValuesForKeys(dic as! [String : AnyObject])
let arrayFriend: NSMutableArray = NSMutableArray()
for friendDic in self.friends! {
let friend : QQFriend = QQFriend.init(dic: friendDic as! NSDictionary)
arrayFriend.add(friend)
}
self.friends = arrayFriend
}
}
import UIKit
//协议
protocol QQHeaderViewDelegate: NSObjectProtocol {
func headerViewDidClickedNameLab(headerView:QQHeaderView)
} class QQHeaderView: UITableViewHeaderFooterView {
weak var delegate:QQHeaderViewDelegate? var nameLabel: UILabel? = {
let nameLabel = UILabel()
return nameLabel
}() var arrow: UIImageView? = {
let arrow = UIImageView.init(image: UIImage(named: "orderDown"))
return arrow
}() var group: QQFriendGroup? {
didSet {
self.nameLabel?.text = group?.name
didMoveToSuperview()
}
} var clickNameBlock: ((Void)->())? = nil
var dividerView : UIView! = nil class func headerViewWithTableView(tableview: UITableView) -> QQHeaderView {
let headerID = "myHeader"
var header = tableview.dequeueReusableHeaderFooterView(withIdentifier: headerID)
if header == nil {
header = QQHeaderView.init(reuseIdentifier: headerID)
}
return header as! QQHeaderView
} override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier) let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(QQHeaderView.nameClick))
self.nameLabel?.isUserInteractionEnabled = true
self.arrow?.isUserInteractionEnabled = true
self.contentView.addGestureRecognizer(tap)
self.contentView.addSubview(nameLabel!)
self.contentView.addSubview(arrow!) let divideView = UIFactory.create_AView()
divideView.backgroundColor = UIColor.hrgb("cccccc")
self.contentView.addSubview(divideView)
self.dividerView = divideView } required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//点击展开或收起列表
func nameClick() {
self.group?.isOpen = !(self.group?.isOpen)!
// if ((self.delegate?.responds(to: Selector(("headerViewDidClickedNameLab:")))) != nil) {
// self.delegate?.headerViewDidClickedNameLab(headerView: self)
// }
if self.clickNameBlock != nil {
self.clickNameBlock!()
}
} override func didMoveToSuperview() {
super.didMoveToSuperview()
if self.group?.isOpen == true {
self.arrow?.transform = CGAffineTransform.init(rotationAngle: .pi)
}else {
self.arrow?.transform = CGAffineTransform.init(rotationAngle: 0)
}
} override func layoutSubviews() {
super.layoutSubviews()
self.nameLabel?.snp.makeConstraints({ (make) in
make.left.equalTo(15)
make.top.equalTo(0)
make.bottom.equalTo(0)
make.right.equalTo(-30)
})
self.arrow?.snp.makeConstraints({ (make) in
make.right.equalTo(-10)
make.width.height.equalTo(14)
make.centerY.equalTo(self.height * 0.5)
})
self.dividerView.snp.makeConstraints { (make) in
make.height.equalTo(0.5)
make.left.right.bottom.equalTo(0)
}
} }
import UIKit
extension ImitationQQ {
func numberOfSections(in tableView: UITableView) -> Int {
return (self.groups?.count)!
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let group: QQFriendGroup = self.groups![section] as! QQFriendGroup
return group.isOpen == true ? group.friends!.count : 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "default", for: indexPath)
cell.textLabel?.text = "\(indexPath.section)" + "\(indexPath.row)"
cell.selectionStyle = .none
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 40
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.01
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header: QQHeaderView = QQHeaderView.headerViewWithTableView(tableview: tableView)
header.delegate = self
header.clickNameBlock = {
self.tabView.reloadData()
}
header.group = self.groups![section] as? QQFriendGroup
return header
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}
}
extension ImitationQQ {
func headerViewDidClickedNameLab(headerView: QQHeaderView) {
self.tabView.reloadData()
}
}
class ImitationQQ: UIViewController, UITableViewDataSource, UITableViewDelegate, QQHeaderViewDelegate {
var tabView: UITableView! = nil
var dividerView : UIView! = nil
lazy var groups : NSArray? = {
var arr : NSMutableArray = NSMutableArray()
for i in 0..<3 {
let group: QQFriendGroup = QQFriendGroup.init(withDic: ["name":"\(i)" + "组", "friends":[["name":"00","intro":"挺好的"],["name":"01","intro":"挺好好的"],["name":"02","intro":"挺好好好的"],["name":"03","intro":"挺好好好好的"]]] as NSDictionary)
arr.add(group)
}
return arr
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.hrgb("f4f4f4")
self.title = "好友列表"
let tabView = UIFactory.create_ATableView(frame: CGRect.init(x: 0, y: 0.5, width: Screen_Width, height: kScreenHeight-64.5), delegate: self, dataSource: self, superView: self.view, type: .plain)
tabView.backgroundColor = UIColor.hrgb("f4f4f4")
tabView.register(UITableViewCell.self, forCellReuseIdentifier: "default")
self.tabView = tabView
let divideView = UIFactory.create_AView()
divideView.frame = CGRect(x: 0, y: 0, width: Screen_Width, height: 0.5)
divideView.backgroundColor = UIColor.hrgb("cccccc")
self.view.addSubview(divideView)
self.dividerView = divideView
}
}
直接粘代码即可运行
swift 实现QQ好友列表功能的更多相关文章
- [iOS基础控件 - 6.9.3] QQ好友列表Demo TableView
A.需求 1.使用plist数据,展示类似QQ好友列表的分组.组内成员显示缩进功能 2.组名使用Header,展示箭头图标.组名.组内人数和上线人数 3.点击组名,伸展.缩回好友组 code so ...
- ExpandableListView仿QQ好友列表
本例中,对ExpandableListView中的数据进行了封装,分为两个JavaBean,一个为Group类表示组信息,一个Child类表示该组下子列表信息: Group: public class ...
- Windows UIA自动化测试框架学习--获取qq好友列表
前段时间应公司要求开发一款针对现有WPF程序的自动化测试工具,在网上查资料找了一段时间,发现用来做自动化测试的框架还是比较多的,比如python的两个模块pywinauto和uiautomation, ...
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)
iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一) 一.项目结构和plist文件 二.实现代码 1.说明: 主控制器直接继承UITableViewController // ...
- 仿QQ好友列表界面的实现
TableView有2种style:UITableViewStylePlain 和 UITableViewStyleGrouped. 但是QQ好友列表的tableView给人的感觉似乎是2个style ...
- (二十七)QQ好友列表的实现
QQ好友列表通过plist读取,plist的结构为一组字典,每个字典内有本组的信息和另外一组字典代表好友. 要读取plist,选择合适的数据结构,例如NSArray,然后调用initWithConte ...
- android 实现QQ好友列表
在某些Android开发群里,看到有些新手问怎么实现QQ好友列表,其实网上一搜挺多的.接触Android,也才一年的时间,大部分时间花在工作上(解bug...),界面上开发很少参与.自己维护的系统应用 ...
- 基于Qt的相似QQ好友列表抽屉效果的实现
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/shuideyidi/article/details/30619167 前段时间在忙毕业设计, ...
- OS开发UI篇—使用UItableview完成一个简单的QQ好友列表
本文转自:http://www.cnblogs.com/wendingding/p/3763330.html 一.项目结构和plist文件 二.实现代码 1.说明: 主控制器直接继承UITableVi ...
随机推荐
- redis 集群环境搭建
原理: 1,每个Redis群集的节点都需要打开两个TCP连接,由于这两个连接就需要两个端口,分别是用于为客户端提供服务的常规Redis TCP命令端口(例如6379)以及通过将10000和命令端口相加 ...
- “TCP:三次握手”分析——以一个简单的“服务器”和“客户端”为例
linux&C这两天学到了网络编程这一章,自己写了一个小的"服务器"和"客户端"程序,目的在于简单理解tcp/ip模型,以及要搭建一台简单服务器,服务器 ...
- Oracle的主要组件和基本概念
oracle 简介 oracle(甲骨文)公司 1977年,三人合伙创办(Software Development Laboratories,SDL) 1979年,更名为Relational Soft ...
- MSSQL SQL注入 总结
0x00 MSSQL 基础 MSSQL系统自带库和表 系统自带库 MSSQL安装后默认带了6个数据库,其中4个系统级库:master,model,tempdb和msdb:2个示例库:Northwind ...
- MacOS修复TNT和谐软件运行崩溃、闪退问题
因为Apple删除了TNT的证书,因此部分应用程序出现了打开崩溃的情况. 目前的解决方案是自己更改签名. 第一种方法: 在终端中运行以下命令:(注意:name.app就是需要更改签名的程序) sudo ...
- Vuex状态管理——任意组件间通信
核心概念 在Vue中实现集中式状态(数据)管理的一个Vue插件,对vue应用中多个组件的共享状态进行集中式的管理(读/写),也是一种组件间通信的方式,且适用于任意组件间通信. 每一个 Vuex 应用的 ...
- 【完虐算法】LeetCode 接雨水问题,全复盘
大家好! 动态规划题目是总结的比较完整了.下面是自从和大家刷开题总结的动态规划解题方法. 今年全国夏天雨是真的多,突然想到今年北京的夏天也不像往年那么热.不知不觉就稳稳地度过了夏天来到秋天. 恰巧前几 ...
- ubuntu图标
linux桌面图标跟windows系统一样,只是个快捷方式,在/usr/share/applications/目录下面有应用程序的启动图标,可以直接复制到桌面,如果这个文件夹下没有的话,可以自己新建一 ...
- Linux——搭建Samba(CIFS)服务器
一.Samba的基本概念 Samba服务:是提供基于Linux和Windows的共享文件服务,服务端和客户端都可以是Linux或Windows操作系统.可以基于特定的用户访问,功能比NFS更强大. S ...
- go 错误处理设计思考
前段时间准备对线上一个golang系统服务进行内部开源,对代码里面的错误处理进行了一波优化. 优化的几个原因: 错误处理信息随意,未分类未定义.看到错误日志不能第一时间定位 错误的日志重复,有时候一个 ...