ios/swift之tableview和collectionview联动

2024-05-31 09:32

本文主要是介绍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联动的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1017687

相关文章

iOS HTTPS证书不受信任解决办法

之前开发App的时候服务端使用的是自签名的证书,导致iOS开发过程中调用HTTPS接口时,证书不被信任 - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAu

vue+elementUI下拉框联动显示

<el-row><el-col :span="12"><el-form-item label="主账号:" prop="partyAccountId" :rules="[ { required: true, message: '主账号不能为空'}]"><el-select v-model="detailForm.partyAccountId" filterable placeholder="

IOS 数组去重的几种方式

本来只知道NSSet和KeyValues的。今天又新学了几种方式 还有就是和同事学的一种方式 外层循环从0开始遍历,内层从最后一个元素开始遍历 for(int i=0;i<index;i++){  for(int j=index-1;j>i;j-- ){ } }

TableView 当前选中的行号。 默认会使哪一行选中 加入导航条后contentInset向下偏移的64

1.得到当前选中的行号     NSLog(@"%ld %s",  [self.tableView indexPathForSelectedRow].row,__func__); 2.默认选中表格的那一行     [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0 ]

iOS Assertion failure in -[UITableView _classicHeightForRowAtIndexPath:]

iOS Assertion failure in -[UITableView _classicHeightForRowAtIndexPath:]  2015-04-24 11:40  956人阅读  评论(0)  收藏  举报   分类:   iOS 基础篇(208)  版权声明:本文为博主原创文章,未经博主允许不得转载。 Assertion

iOS:编译时出现no such file or directory:xxx以及use twice...filenames are used to distinguish private dec

简    注册  登录   添加关注 作者  婉卿容若 2016.04.29 11:22 写了21870字,被16人关注,获得了14个喜欢 iOS:编译时出现"no such file or directory:xxx"以及"use twice...filenames are used to distinguish private

iOS 到处 ipa包的时候 会有四个选项分别代表什么

如图 在 iOS 到处 ipa包的时候 会有四个选项  1.Save for iOS App Store Deployment 保存到本地 准备上传App Store 或者在越狱的iOS设备上使用 2.Save for Ad Hoc Deployment 保存到本地 准备在账号添加的可使用设备上使用(具体为在开发者账户下添加可用设备的udid),该app包是发布证书编

iOS 7适配上存在的各种问题

谈谈项目中遇到的各种iOS7适配问题 由于我的项目要适配到iOS7.1, 而现在已经是9时代了,在实际工作中我也是遇到了各种奇葩的坑,所以我想尽快把遇到的iOS7适配问题和解决方案分享出来,以后这些东西可能就用处不大了。   1.字体问题 iOS7中的字体适配恐怕是最麻烦的坑了,原因是iOS7以上的许多字体在7都是不存在的,甚至包括一些system-字体。比如system-

潜艇伟伟迷杂交版植物大战僵尸2024最新免费安卓+ios苹果+iPad分享

嗨,亲爱的游戏迷们!今天我要给你们种草一个超有趣的游戏——植物大战僵尸杂交版。这款游戏不仅继承了原有经典游戏的核心玩法,还加入了许多创新元素,让玩家能够体验到前所未有的乐趣。快来跟随我一起探索这个神奇的世界吧! 植物大战僵尸杂交版最新绿色版下载链接: https://pan.quark.cn/s/d60ed6e4791c 🔥 创新与经典的完美结合 植物大战僵尸杂交版在保持了原游戏经典玩

浏览器在iOS或Android中的一些方法

判断当前应用 var deviceType="H5"if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {deviceType='ios'} else if (/(Android)/i.test(navigator.userAgent)) {// alert("Android");deviceType='android'} else