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 ...
随机推荐
- 西邮Linux兴趣小组第一次技术分享会
2016年10月30日晚,西邮Linux兴趣小组技术分享会在西安邮电大学长安校区东区逸夫教学楼FF305室成功举办.200多名来自全校不同专业的15,16级同学参加了此次分享会. 分享会于20:00正 ...
- 03 | 变量的解构赋值 | es6
变量的解构赋值 数组的解构赋值 基本用法 ES6 允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构(Destructuring). 以前,为变量赋值,只能直接指定值. let a ...
- js分支语句
一.逻辑分支(选择结构,分支结构) 其实今天的课程才算开始涉及到逻辑 程序的三大结构 顺序结构 - 每天 代码逐行执行,一行一行自上而下执行 分支结构 有选择了,十字路口的选择,只能选择一个,如果.. ...
- C# | VS2019连接MySQL的三种方法以及使用MySQL数据库教程
本文将介绍3种添加MySQL引用的方法,以及连接MySQL和使用MySQL的教程 前篇:Visual Studio 2019连接MySQL数据库详细教程 \[QAQ \] 第一种方法 下载 Mysql ...
- Ubuntu系统下《汇编语言》环境配置
说明 1.系统:Ubuntu codists@pc:~$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Des ...
- 这一次,Google 终于对 Web 自动化下手了!
大家好,我是安果! 最近 Google 对 Chrome 进行了一次比较大的更新,其中一项是脚本录制回放功能,它可以非常方便我们处理一些自动化场景 我们可以在 Chrome 官网下载 Chrome C ...
- 详解电子表格中的json数据:序列化与反序列化
从XML到JSON 当下应用开发常见的B/S架构之下,我们会遇到很多需要进行前后端数据传输的场景.而在这个传输的过程中,数据通过何种格式传输.方式是否迅速便捷.书写方式是否简单易学,都成为了程序员在开 ...
- 0-pyqt介绍
1.QT 的特点 2.QT的历史 3.搭建pyQT的开发环境 python pyqt包 pycharm 4.搭建pyQT第一个应用 必须使用两个类:QApplication和QWidget.都在P ...
- CTF入门学习4->前端HTML基础
Web安全基础 02 前端开发-HTML基础 浏览器对于上网者来说是一种直观.可视化的呈现.服务器发送数据到客户端,客户端需要处理这些数据,互联网就造就了这种数据语言--HTML. 02-00 概述 ...
- [loj3014]独特的城市
约定:一棵树的深度定义为其中到根最远的点到根的距离 考虑节点$x$的答案: 任取一条直径,根据直径的性质,到$x$较远的直径端点一定是到$x$最远的点之一 由此,不难证明对于$x$独特的点,一定在$x ...