博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios/swift之tableview和collectionview联动
阅读量:4291 次
发布时间:2019-05-27

本文共 13260 字,大约阅读时间需要 44 分钟。

/** 电商领域的左边的tableview和右边的collectionview联动 */import UIKit@available(iOS 11.0, *)class LYBTabviewAndCollectionviewConnectview: UIView {    var tabindex:Int=0//当前选中的左边tablecell    override init(frame: CGRect) {        super.init(frame: frame)        addSubview(lefttabview)                   var leftkeyarr:[String]=[]//解析出第一层左边显示的数据        var rightFirstfloorValueArr:[[[String:[String]]]]=[]//第一层右边的数据        for (key,value) in rightdata{            leftkeyarr.append(key)            rightFirstfloorValueArr.append(value)        }        leftkeyarr =  leftkeyarr.reversed()//吧数组倒序排列        print("\(leftkeyarr)")        lefttabview.titleArr=leftkeyarr//左边tableview标题        addSubview(rightcollectionview)            //解析出第二层数据,组头和图片数据        // 先去默认数据第一组数据        rightFirstfloorValueArr = rightFirstfloorValueArr.reversed()        let secDictArr:[[String:[String]]] = rightFirstfloorValueArr[0]        let  secmodelArr:[LYBrightSecModel]=[LYBrightSecModel].deserialize(from: secDictArr)! as! [LYBrightSecModel]        print("\(String(describing: secmodelArr.first?.headerName?.first))")        rightcollectionview.modelArr = secmodelArr//重写属性,刷新右边的数据                //选中tablecell,刷新collection view的数据        lefttabview.selectcellBlock={            (index)->()in            self.tabindex=index            let secDictArr:[[String:[String]]] = rightFirstfloorValueArr[self.tabindex]            let  secmodelArr:[LYBrightSecModel]=[LYBrightSecModel].deserialize(from: secDictArr)! as! [LYBrightSecModel]            print("\(String(describing: secmodelArr.first?.headerName?.first))")            self.rightcollectionview.modelArr = secmodelArr//重写属性,刷新右边的数据        }                }        required init?(coder aDecoder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }        //左边的tableview    lazy var lefttabview:LYBLefttabview={        let lefttabv:LYBLefttabview=LYBLefttabview.init(frame: CGRect.init(x: 0, y:0, width: 100, height: Int(HEIGHT) - Int(TopSpaceHigh)-Int(bottomSafeHeight)))        return lefttabv    }()        //右边的collectionview    lazy var rightcollectionview:LYBrightCollectionview={        let rightcollectionv:LYBrightCollectionview=LYBrightCollectionview.init(frame: CGRect.init(x: 110, y:0, width: Int(WIDTH-110), height: Int(HEIGHT) - Int(TopSpaceHigh)-Int(bottomSafeHeight)))        return rightcollectionv    }()        //第一层是左边显示的数据,下面是右边显示的数据,    lazy var rightdata:[String:[[String:[String]]]]={        let dataDict:[String:[[String:[String]]]]=["leftone":[["headerName":["one第一组"],"images":["icon_shot_sel","icon_shot_sel","icon_shot_sel","icon_shot_sel","icon_shot_sel","icon_shot_sel"]],["headerName":["one第二组"],"images":["comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark"]],["headerName":["one第三组"],"images":["comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark"]]],                                                   "lefttwo":[["headerName":["two第一组"],"images":["icon_shot_sel","icon_shot_sel","icon_shot_sel","icon_shot_sel","icon_shot_sel","icon_shot_sel"]],["headerName":["two第二组"],"images":["comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark"]],["headerName":["two第三组"],"images":["comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark"]]],                                                   "leftthree":[["headerName":["three第一组"],"images":["icon_shot_sel","icon_shot_sel","icon_shot_sel","icon_shot_sel","icon_shot_sel","icon_shot_sel"]],["headerName":["three第二组"],"images":["comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark"]],["headerName":["three第三组"],"images":["comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark","comm_btn_checkmark"]]]                     ]                return dataDict    }()      }*******tabview/*** 左边的tableview **/import UIKit@available(iOS 11.0, *)class LYBLefttabview: UIView ,UITableViewDelegate,UITableViewDataSource{        //标题数组    var titleArr:[String]=[""]{        didSet{            tab.reloadData()        }    }        //点击cell触发    var selectcellBlock:(Int)->()={        (Int)->()        in    }    override init(frame: CGRect) {        super.init(frame: frame)        setTable()            }        required init?(coder aDecoder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }        func numberOfSections(in tableView: UITableView) -> Int {        return 1    }        func tableView(_ tableView:UITableView, numberOfRowsInSection section: Int) -> Int {        return titleArr.count;    }        func tableView(_ tableView:UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {        let cell:LYBLefttabcell=tableView.dequeueReusableCell(withIdentifier:"LYBLefttabcell")as!LYBLefttabcell        if(titleArr.count>0){            cell.titlestr=titleArr[indexPath.row]        }                return cell    }        func tableView(_ tableView:UITableView, didSelectRowAt indexPath: IndexPath) {        tableView.deselectRow(at: indexPath, animated:true)      selectcellBlock(indexPath.row)        print("点击")    }    //cell高度    func tableView(_ tableView:UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {        return 50    }               func setTable() {        tab.tableFooterView=UIView.init()        tab.delegate=self        tab.dataSource=self        addSubview(tab)        tab.separatorStyle=UITableViewCell.SeparatorStyle.none                //注册cell---UITableViewCell.classForCoder()或者UITableViewCell.self        tab.register(LYBLefttabcell.self, forCellReuseIdentifier:"LYBLefttabcell")        tab.showsVerticalScrollIndicator=false        tab.contentInsetAdjustmentBehavior=UIScrollView.ContentInsetAdjustmentBehavior.never                  }           //懒加载---tab    lazy var tab:UITableView = {        var tabv=UITableView.init(frame:CGRect(x:0,y:0,width:100,height:Int(HEIGHT)-49+Int(TopSpaceHigh)-Int(bottomSafeHeight)), style: UITableView.Style.plain)                return tabv    }()        }*******tabviewcell/***   左边tableviewcell */import UIKitclass LYBLefttabcell: UITableViewCell {    var imageStr:String="appstart"{                didSet{            imagev.image=UIImage.init(named: imageStr)        }    }        var titlestr:String=""{        didSet{            tetxtlab.text=titlestr        }    }            override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {        super.init(style: style, reuseIdentifier: reuseIdentifier)        initViews()    }        required init?(coder aDecoder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }            func initViews(){//        addSubview(imagev)        addSubview(tetxtlab)        addSubview(horizontalLine)// 水平分割线        addSubview(verticalLine)//竖分割线    }        //左边的图片    lazy  var imagev:UIImageView={        let imageV:UIImageView = UIImageView.init(frame: CGRect.init(x: 20, y: 10, width: 30, height:30))        return imageV    }()    //中间的标题    lazy var tetxtlab:UILabel={        let textlbl:UILabel=UILabel.init(frame: CGRect.init(x: 0, y: 0, width: 100, height: 50))        textlbl.textAlignment=NSTextAlignment.center        return textlbl    }()           //水平分割线    lazy var horizontalLine:UIView={        let horiLine:UIView=UIView.init(frame: CGRect.init(x: 0, y: 49, width: self.frame.size.width, height: 1))        horiLine.backgroundColor=UIColor.gray        return horiLine    }()        // 竖分割线    lazy var verticalLine:UIView = {            let vertiline:UIView=UIView.init(frame: CGRect.init(x: 99, y: 0, width: 1, height: 50))        vertiline.backgroundColor=UIColor.gray            return vertiline    }()}*******collectionview/** 右边的collectioview */import UIKit@available(iOS 11.0, *)class LYBrightCollectionview: UIView ,UICollectionViewDelegate,UICollectionViewDataSource {    var collec:UICollectionView?    //图片数组    var modelArr:[AnyObject]=[]{                didSet{            collec?.reloadData()        }    }              override init(frame: CGRect) {        super.init(frame: frame)        createCollectionView()            }        required init?(coder aDecoder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }        func createCollectionView(){        let flowLayout = UICollectionViewFlowLayout.init()        flowLayout.itemSize=CGSize.init(width:(WIDTH-110)/3, height:(WIDTH-110)/3)        flowLayout.minimumLineSpacing=0        flowLayout.minimumInteritemSpacing=0        flowLayout.headerReferenceSize = CGSize.init(width: WIDTH, height: 50)//组头的高度        collec = UICollectionView.init(frame: CGRect.init(x: 0, y: 0, width: Int(WIDTH-110), height: Int(HEIGHT)-Int(bottomSafeHeight)-Int(TopSpaceHigh)), collectionViewLayout: flowLayout)        collec?.backgroundColor=UIColor.white        collec?.delegate=self        collec?.dataSource=self        collec?.register(LYBrightcolllectioncell.classForCoder(), forCellWithReuseIdentifier: "LYBrightcolllectioncell")                //注册组头        collec!.register(LYBRightcolectionHeaderview.classForCoder(), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "LYBRightcolectionHeaderview")        addSubview(collec!)    }        func numberOfSections(in collectionView: UICollectionView) -> Int {        return modelArr.count    }        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {        let model:LYBrightSecModel=modelArr[section] as! LYBrightSecModel                return (model.images?.count)!    }        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {                let cell:LYBrightcolllectioncell = collectionView.dequeueReusableCell(withReuseIdentifier: "LYBrightcolllectioncell", for: indexPath) as! LYBrightcolllectioncell                cell.backgroundColor=UIColor.red        return cell    }        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {        print("点击colleccell")    }    //UICollectionElementKindSectionHeader和 kind 比较来区分header 和footer    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {        var headerView : LYBRightcolectionHeaderview = (collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "LYBRightcolectionHeaderview", for: indexPath as IndexPath) as? LYBRightcolectionHeaderview)!        //分区头        if kind == UICollectionView.elementKindSectionHeader{            headerView = (collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "LYBRightcolectionHeaderview", for: indexPath as IndexPath) as? LYBRightcolectionHeaderview)!            let model:LYBrightSecModel=modelArr[indexPath.section] as! LYBrightSecModel            headerView.headertitle=(model.headerName?.first)!        }            //分区尾        else if kind == UICollectionView.elementKindSectionFooter{                    }        return headerView        }}********collectionviewcell/**  右边的collectioviewcell */import UIKitclass LYBrightcolllectioncell: UICollectionViewCell {    var imageV:UIImageView!    var image:String="appstart"{        willSet(image) {                    }        didSet {                        imageV.image=UIImage.init(named: image)        }            }        override init(frame: CGRect) {        super.init(frame: frame)        createCell()    }        required init?(coder aDecoder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }            func createCell(){                imageV = UIImageView.init(frame:CGRect.init(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))        imageV.image=UIImage.init(named: "appstart")        addSubview(imageV)    }}*****collectionviewheader/** 右边的collectionviewHeader */import UIKitclass LYBRightcolectionHeaderview: UICollectionReusableView {    var headertitle:String=""{        didSet{            lab.text=headertitle        }    }    override init(frame: CGRect) {        super.init(frame: frame)        initViews()    }        func initViews(){        backgroundColor=UIColor.gray        addSubview(lab)    }        required init?(coder aDecoder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }    lazy var lab:UILabel={        let lbl=UILabel.init(frame: CGRect.init(x: 0, y: 0, width: WIDTH, height: self.frame.size.height))        lbl.text="便民"        return lbl    }()}*****第一层模型/** tableview和collectioncview的model */import UIKitclass LYBTabAndCollectionConnectModel: HandyJSON {    var leftone:[LYBrightSecModel]?     var lefttwo:[LYBrightSecModel]?     var leftthree:[LYBrightSecModel]?        required init(){            }}****第二层模型/**  右边第二层模型  */import UIKitclass LYBrightSecModel: HandyJSON {    var headerName:[String]?    var images:[String]?    required init(){        }}

 

转载地址:http://bmmgi.baihongyu.com/

你可能感兴趣的文章
安卓三状态切换按钮TriStateToggleButton
查看>>
Spring框架-AOP细节
查看>>
java.lang.Instrument 代理Agent使用
查看>>
Javascript:指针、帽子和女朋友
查看>>
Android中的ALERTDIALOG使用_优就业
查看>>
java使用javacsv读取csv文件 导入Mysql数据库
查看>>
Toolbar 不为人知的助手,ActionMenuView
查看>>
Java回调方法详解
查看>>
如何获取url中的参数并传递给iframe中的报表
查看>>
以前端架构师的思想看问题:解决单页应用,系统角色请求抢占session发送请求问题
查看>>
jsessionid问题解决方案
查看>>
MySQL和Oracle的delete,truncate
查看>>
idea使用心得(2)-安装设置与创建web项目
查看>>
将项目放到服务器tomcat上运行
查看>>
JS几种变量交换方式以及性能分析对比
查看>>
Android面试一天一题(6)——如何进行技术选型
查看>>
Velocity笔记--使用Velocity获取动态Web项目名的问题
查看>>
安卓学习:如何将一个Activity设置成窗口的样式
查看>>
Android: 自定义 View
查看>>
MySQL-99语法:外连接与内连接-(左内连接多表和查询)
查看>>