/**
电商领域的左边的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 UIKit
class 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 UIKit
class 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 UIKit
class 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 UIKit
class LYBTabAndCollectionConnectModel: HandyJSON {
var leftone:[LYBrightSecModel]?
var lefttwo:[LYBrightSecModel]?
var leftthree:[LYBrightSecModel]?
required init(){
}
}
****第二层模型
/**
右边第二层模型
*/
import UIKit
class LYBrightSecModel: HandyJSON {
var headerName:[String]?
var images:[String]?
required init(){
}
}