美文网首页
Swift 实现QQ好友列表功能

Swift 实现QQ好友列表功能

作者: MJBaby | 来源:发表于2017-06-19 16:46 被阅读91次

    整理了一下实现QQ好友列表的功能,数据也是自己手动拼接模拟的
    QQFriend 存储好友数据信息

    
    class QQFriend: NSObject {
    
    var name: String?
    
    var intro: String?
    
    init(dic: NSDictionary) {
    
    super.init()
    
    self.setValuesForKeys(dic as! [String : AnyObject])
    
    }
    
    }
    
    

    QQFriendGroup 存储好友分组数据

    
    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
    
    }
    
    }
    
    

    QQHeaderView 分组View

    
    //协议
    
    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)
    
    }
    
    }
    
    }
    
    

    ImitationQQ 控制器 实现功能

    
    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
    
    }
    
    }
    
    

    不太会排版,这么贴代码好low,下次改进一下。

    相关文章

      网友评论

          本文标题:Swift 实现QQ好友列表功能

          本文链接:https://www.haomeiwen.com/subject/wotnqxtx.html