最近项目中有类似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
}
}
直接粘代码即可运行
手机扫一扫
移动阅读更方便
你可能感兴趣的文章