本文主要是介绍ios/swift之tableview和collectionview联动,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/**电商领域的左边的tableview和右边的collectionview联动*/import UIKit@available(iOS 11.0, *)
class LYBTabviewAndCollectionviewConnectview: UIView {var tabindex:Int=0//当前选中的左边tablecelloverride 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)->()inself.tabindex=indexlet 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")}//左边的tableviewlazy 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}()//右边的collectionviewlazy 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!LYBLefttabcellif(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=selftab.dataSource=selfaddSubview(tab)tab.separatorStyle=UITableViewCell.SeparatorStyle.none//注册cell---UITableViewCell.classForCoder()或者UITableViewCell.selftab.register(LYBLefttabcell.self, forCellReuseIdentifier:"LYBLefttabcell")tab.showsVerticalScrollIndicator=falsetab.contentInsetAdjustmentBehavior=UIScrollView.ContentInsetAdjustmentBehavior.never}//懒加载---tablazy 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.centerreturn 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.grayreturn horiLine}()// 竖分割线lazy var verticalLine:UIView = {let vertiline:UIView=UIView.init(frame: CGRect.init(x: 99, y: 0, width: 1, height: 50))vertiline.backgroundColor=UIColor.grayreturn 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=0flowLayout.minimumInteritemSpacing=0flowLayout.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.whitecollec?.delegate=selfcollec?.dataSource=selfcollec?.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! LYBrightSecModelreturn (model.images?.count)!}func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {let cell:LYBrightcolllectioncell = collectionView.dequeueReusableCell(withReuseIdentifier: "LYBrightcolllectioncell", for: indexPath) as! LYBrightcolllectioncellcell.backgroundColor=UIColor.redreturn cell}func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {print("点击colleccell")}//UICollectionElementKindSectionHeader和 kind 比较来区分header 和footerfunc 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! LYBrightSecModelheaderView.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.grayaddSubview(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(){}
}
这篇关于ios/swift之tableview和collectionview联动的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!