IOS 20 发现界面(UITableView)歌单列表(UICollectionView)实现

本文主要是介绍IOS 20 发现界面(UITableView)歌单列表(UICollectionView)实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

发现界面完整效果

本文实现歌单列表效果

文章基于 IOS 19 发现界面(UITableView)快捷按钮实现 继续实现发现界面歌单列表效果

歌单列表Cell实现

实现流程:

1.创建Cell,及在使用UITableView的Controller控制器上注册Cell;

2.获取data列表数据,并调用UITableView的reloadData(),将数据更新到列表;

3.将data的Item数据绑定UITableView的每一个Cell。

1)创建和注册Cell

从效果图上面可以看出,歌单列表Cell由一个title + 宫格列表View来实现的,这里宫格列表使用UICollectionView来实现。需要注意的是,UICollectionView本身也是一个可滑动的控件,把UICollectionView内嵌到UITableView中,且需要显示UICollectionView的全部Item;那么就需要设置UICollectionView不允许滑动,且UICollectionView高度 == Item高度*行数。

通过懒加载创建ItemTitleView 和 UICollectionView,自定义ItemTitleView,就是TGRelativeLayout包含title和右边的icon,并封装UICollectionView到ViewFactoryUtil.collectionView()中。

    /// 标题控件lazy var titleView: ItemTitleView = {let r = ItemTitleView()r.titleView.text = R.string.localizable.recommendSheet()return r}()lazy var collectionView: UICollectionView = {let r = ViewFactoryUtil.collectionView()r.delegate = selfr.dataSource = selfr.isScrollEnabled = falsereturn r}()
//
//  ItemTitleView.swift
//  首页-发现界面-歌单组/推荐单曲组 标题view
//
//  Created by jin on 2024/8/29.
//import UIKitimport TangramKitclass ItemTitleView : TGRelativeLayout{init(){super.init(frame: CGRect.zero)initViews()}required init?(coder: NSCoder) {super.init(coder: coder)initViews()}func initViews(){tg_width.equal(.fill)tg_height.equal(.wrap)tg_padding = UIEdgeInsets(top: PADDING_MEDDLE, left: PADDING_OUTER, bottom: PADDING_MEDDLE, right: PADDING_OUTER)addSubview(titleView)addSubview(moreIconView)}lazy var titleView: UILabel = {let r = UILabel()r.tg_width.equal(.wrap)r.tg_height.equal(.wrap)r.tg_centerY.equal(0)r.numberOfLines = 1r.font = UIFont.boldSystemFont(ofSize: TEXT_LARGE2)r.textColor = .colorOnSurfacereturn r}()lazy var moreIconView: UIImageView = {let r = UIImageView()r.tg_width.equal(15)r.tg_height.equal(15)r.image = R.image.superChevronRight()?.withTintColor()r.tintColor = .black80r.tg_right.equal(0)r.tg_centerY.equal(0)//图片完全显示到控件里面r.contentMode = .scaleAspectFitreturn r}()
}
    /// 创建CollectionViewstatic func collectionView() -> UICollectionView {let r = UICollectionView(frame: CGRect.zero, collectionViewLayout: collectionViewFlowLayout())r.backgroundColor = .clear//不显示滚动条r.showsVerticalScrollIndicator = falser.showsHorizontalScrollIndicator = false//collectionView的内容从collectionView顶部距离开始显示,不要自动偏移状态栏尺寸r.contentInsetAdjustmentBehavior = .neverr.tg_width.equal(.fill)r.tg_height.equal(.fill)return r}

重写,添加ItemTitleView 和 UICollectionView到SheetGroupCell

class SheetGroupCell:BaseTableViewCell{static let NAME = "SheetGroupCell"var datum:Array<Sheet> = []override func initViews() {super.initViews()//分割线container.addSubview(ViewFactoryUtil.smallDivider())//标题container.addSubview(titleView)container.addSubview(collectionView)}override func getContainerOrientation() -> TGOrientation {return .vert}
}

绑定宫格列表数据,动态计算Item宽高。

    func bind(_ data:SheetData) {//计算每个cell宽度//屏幕宽度-外边距16*2-(self.spanCount-1)*5cellWidth = (SCREEN_WIDTH-PADDING_OUTER*CGFloat(2) - (spanCount - CGFloat(1))*PADDING_SMALL)/spanCount//cell高度,5:图片和标题边距,40:2行文字高度cellHeight = cellWidth + PADDING_SMALL + 40//计算可以显示几行let rows = ceil(CGFloat(data.datum.count) / spanCount)//CollectionView高度等于,行数*行高,10:垂直方向每个cell间距let viewHeight = rows * (cellHeight + PADDING_MEDDLE)collectionView.tg_height.equal(viewHeight)datum.removeAll()datum = data.datumcollectionView.reloadData()}

注册SheetGroupCell

class DiscoveryController: BaseLogicController {override func initViews() {super.initViews()setBackgroundColor(.colorBackgroundLight)//初始化TableView结构initTableViewSafeArea()//注册celltableView.register(BannerCell.self, forCellReuseIdentifier: Constant.CELL)tableView.register(ButtonCell.self, forCellReuseIdentifier: ButtonCell.NAME)tableView.register(SheetGroupCell.self, forCellReuseIdentifier: SheetGroupCell.NAME)}
}

2)获取data列表数据

定义宫格列表数据模型SheetData

//
//  SheetData.swift
//  发现界面歌单数据
//
//  Created by jin on 2024/8/29.
//import Foundationclass SheetData{var datum:[Sheet]!init(_ datum: [Sheet]!) {self.datum = datum}
}

请求接口获取歌单列表数据,更新tableView.reloadData()

class DiscoveryController: BaseLogicController {override func initViews() {super.initViews()setBackgroundColor(.colorBackgroundLight)//初始化TableView结构initTableViewSafeArea()//注册celltableView.register(BannerCell.self, forCellReuseIdentifier: Constant.CELL)tableView.register(ButtonCell.self, forCellReuseIdentifier: ButtonCell.NAME)tableView.register(SheetGroupCell.self, forCellReuseIdentifier: SheetGroupCell.NAME)}override func initDatum() {super.initDatum()loadData()}func loadData() {DefaultRepository.shared.bannerAds().subscribeSuccess { [weak self] data in//清除原来的数据self?.datum.removeAll()//添加轮播图self?.datum.append(BannerData(data:data.data!.data!))//添加快捷按钮self?.datum.append(ButtonData())//请求歌单数据self?.loadSheetsData()}.disposed(by: rx.disposeBag)}/// 请求歌单数据func loadSheetsData() {DefaultRepository.shared.sheets(size: VALUE12).subscribeSuccess { [weak self] data in//添加歌单数据self?.datum.append(SheetData(data.data!.data!))self?.tableView.reloadData()}.disposed(by: rx.disposeBag)}
}

3)Item数据绑定Cell

DiscoveryController控制器重写父类的扩展 cellForRowAt方法,创建对应的Cell,并将Item数据绑定到Cell。

extension DiscoveryController{// 返回当前位置cell/// - Parameters:///   - tableView: <#tableView description#>///   - indexPath: <#indexPath description#>/// - Returns: <#description#>override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {let data = datum[indexPath.row]//获取当前Cell的类型let type = typeForItemAtData(data)switch(type){case .button://按钮let cell = tableView.dequeueReusableCell(withIdentifier:  ButtonCell.NAME, for: indexPath) as! ButtonCellcell.bind(data as! ButtonData)return cellcase .sheet://歌单let cell = tableView.dequeueReusableCell(withIdentifier:  SheetGroupCell.NAME, for: indexPath) as! SheetGroupCellcell.bind(data as! SheetData)return celldefault://banner//取出一个Celllet cell = tableView.dequeueReusableCell(withIdentifier:  Constant.CELL, for: indexPath) as! BannerCell//绑定数据cell.bind(data as! BannerData)cell.bannerClick = {[weak self] data inprint("bannerClick \(data)")}return cell}}
}

歌单UICollectionView Cell实现

由于UICollectionView也是一个列表控件,实现UICollectionView的Cell的流程跟 实现UITableView的Cell的流程基本类似。

实现流程:

1.创建Cell,及在使用UICollectionView的View上注册Cell;

2.获取data列表数据,并调用UICollectionView的reloadData(),将数据更新到列表;

3.将data的Item数据绑定UICollectionView的每一个Cell;

4.重写UICollectionViewDelegateFlowLayout,计算Cell的尺寸。

1)创建和注册Cell

从效果图上面可以看出,歌单列表Item的Cell由一个垂直TGLinearLayout包含UIImageView + UILabel来实现的。布局比较简单,实现代码如下:

//
//  SheetCell.swift
//  歌单cell
//
//  Created by jin on 2024/8/29.
//import Foundationclass SheetCell : BaseCollectionViewCell{override func initViews() {super.initViews()container.tg_space = PADDING_SMALLcontainer.addSubview(iconView)container.addSubview(titleView)}func bind(_ data:Sheet) {//OC库显示图片
//        if let r = data.icon {
//            let r = ResourceUtil.resourceUri(r)
//            iconView.sd_setImage(with: URL(string: r), placeholderImage: R.image.placeholder())
//        }iconView.show(data.icon)titleView.text = data.title}lazy var iconView: UIImageView = {let r = UIImageView()r.tg_width.equal(.fill)r.tg_height.equal(r.tg_width)r.image = R.image.placeholder()//图片从中心等比向外面填充,控件没有黑边,但图片可能被裁剪r.contentMode = .scaleAspectFill//小圆角r.smallCorner()return r}()/// 标题lazy var titleView: UILabel = {let r = UILabel()r.tg_width.equal(.fill)r.tg_height.equal(.wrap)r.numberOfLines = 2r.font = UIFont.systemFont(ofSize:TEXT_MEDDLE)r.textColor = .colorOnSurfacereturn r}()
}

 2)获取data列表数据

定义宫格列表Item数据模型Sheet

//
//  Sheet.swift
//  歌单对象
//
//  Created by jin on 2024/8/23.
//import Foundation//导入JSON解析框架
import HandyJSONclass Sheet : HandyJSON{/// 歌单标题var title:String!/// 歌单封面var icon:String?/// 点击数var clicksCount:Int=0/// 收藏数var collectsCount:Int=0/// 评论数var commentsCount:Int=0/// 音乐数量var songsCount:Int=0/// 歌单创建者
//    var user:User!
//
//    /// 歌曲列表
//    var songs:Array<Song>?var detail:String?required init() {}
}

从SheetGroupCell bind()中获取歌单宫格列表Item数据,更新collectionView.reloadData()

class SheetGroupCell:BaseTableViewCell{static let NAME = "SheetGroupCell"var datum:Array<Sheet> = []var cellWidth:CGFloat!var cellHeight:CGFloat!var spanCount:CGFloat = 3override func initViews() {super.initViews()//分割线container.addSubview(ViewFactoryUtil.smallDivider())//标题container.addSubview(titleView)container.addSubview(collectionView)collectionView.register(SheetCell.self, forCellWithReuseIdentifier: Constant.CELL)}override func getContainerOrientation() -> TGOrientation {return .vert}func bind(_ data:SheetData) {//计算每个cell宽度//屏幕宽度-外边距16*2-(self.spanCount-1)*5cellWidth = (SCREEN_WIDTH-PADDING_OUTER*CGFloat(2) - (spanCount - CGFloat(1))*PADDING_SMALL)/spanCount//cell高度,5:图片和标题边距,40:2行文字高度cellHeight = cellWidth + PADDING_SMALL + 40//计算可以显示几行let rows = ceil(CGFloat(data.datum.count) / spanCount)//CollectionView高度等于,行数*行高,10:垂直方向每个cell间距let viewHeight = rows * (cellHeight + PADDING_MEDDLE)collectionView.tg_height.equal(viewHeight)datum.removeAll()datum = data.datumcollectionView.reloadData()}
}

3)Item数据绑定Cell

SheetGroupCell 重写父类的扩展 cellForItemAt方法,创建对应的Cell,并将Item数据绑定到Cell。

/// CollectionView数据源和代理
extension SheetGroupCell:UICollectionViewDataSource,UICollectionViewDelegate{/// 有多少个func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {return datum.count}/// 返回cellfunc collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {let data = datum[indexPath.row]let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Constant.CELL, for: indexPath) as! SheetCellcell.bind(data)return cell}func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {if let d = delegate {d.sheetCLick(data: datum[indexPath.row])}}
}

4)计算Cell尺寸

重写UICollectionViewDelegateFlowLayout,返回Cell的尺寸

/// UICollectionViewDelegateFlowLayout
extension SheetGroupCell:UICollectionViewDelegateFlowLayout{/// 返回CollectionView里面的Cell到CollectionView的间距func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {return UIEdgeInsets(top: 0, left: PADDING_OUTER, bottom: PADDING_OUTER, right: PADDING_OUTER)}/// 返回每个Cell的行间距func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {return PADDING_MEDDLE}/// 返回每个Cell的列间距func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {return PADDING_SMALL}/// cell尺寸func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {return CGSize(width: cellWidth, height: cellHeight)}
}

至此完成歌单列表的实现。

这篇关于IOS 20 发现界面(UITableView)歌单列表(UICollectionView)实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

python实现svg图片转换为png和gif

《python实现svg图片转换为png和gif》这篇文章主要为大家详细介绍了python如何实现将svg图片格式转换为png和gif,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录python实现svg图片转换为png和gifpython实现图片格式之间的相互转换延展:基于Py

Python利用ElementTree实现快速解析XML文件

《Python利用ElementTree实现快速解析XML文件》ElementTree是Python标准库的一部分,而且是Python标准库中用于解析和操作XML数据的模块,下面小编就来和大家详细讲讲... 目录一、XML文件解析到底有多重要二、ElementTree快速入门1. 加载XML的两种方式2.

Java的栈与队列实现代码解析

《Java的栈与队列实现代码解析》栈是常见的线性数据结构,栈的特点是以先进后出的形式,后进先出,先进后出,分为栈底和栈顶,栈应用于内存的分配,表达式求值,存储临时的数据和方法的调用等,本文给大家介绍J... 目录栈的概念(Stack)栈的实现代码队列(Queue)模拟实现队列(双链表实现)循环队列(循环数组

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Python实现图片分割的多种方法总结

《Python实现图片分割的多种方法总结》图片分割是图像处理中的一个重要任务,它的目标是将图像划分为多个区域或者对象,本文为大家整理了一些常用的分割方法,大家可以根据需求自行选择... 目录1. 基于传统图像处理的分割方法(1) 使用固定阈值分割图片(2) 自适应阈值分割(3) 使用图像边缘检测分割(4)

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

C# foreach 循环中获取索引的实现方式

《C#foreach循环中获取索引的实现方式》:本文主要介绍C#foreach循环中获取索引的实现方式,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、手动维护索引变量二、LINQ Select + 元组解构三、扩展方法封装索引四、使用 for 循环替代

Spring Security+JWT如何实现前后端分离权限控制

《SpringSecurity+JWT如何实现前后端分离权限控制》本篇将手把手教你用SpringSecurity+JWT搭建一套完整的登录认证与权限控制体系,具有很好的参考价值,希望对大家... 目录Spring Security+JWT实现前后端分离权限控制实战一、为什么要用 JWT?二、JWT 基本结构

Java实现优雅日期处理的方案详解

《Java实现优雅日期处理的方案详解》在我们的日常工作中,需要经常处理各种格式,各种类似的的日期或者时间,下面我们就来看看如何使用java处理这样的日期问题吧,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言一、日期的坑1.1 日期格式化陷阱1.2 时区转换二、优雅方案的进阶之路2.1 线程安全重构2

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络