ios swift5 collectionView 瀑布流(兩列)

文章目錄

  • 1.瀑布流
    • 1.1 demo地址
    • 1.2 記得把部署的最低版本由8改成11,13甚至更高。不然編譯會報錯
  • 2.動態計算圖片和文字的高度

1.瀑布流

1.1 demo地址

CollectionViewWaterfallLayout - github

請添加圖片描述

1.2 記得把部署的最低版本由8改成11,13甚至更高。不然編譯會報錯

請添加圖片描述

2.動態計算圖片和文字的高度

//可以正常使用
import UIKit
import SnapKit
class ConcernedVC: UICollectionViewController, UICollectionViewDelegateFlowLayout {let cellReuseIdentifier = "WaterfallCell"let itemsPerRow: CGFloat = 2let sectionInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)let itemSpacing: CGFloat = 10 // Spacing between items in the same columnvar columnHeights: [CGFloat] = [0, 0] // Heights of the two columnslet sampleData: [(image: UIImage, text: String)] = [(UIImage(named: "img_about us_app")!, "Sample Text 1"),(UIImage(named: "banner")!, "Sample Text 2adfahdfkajdfiahdofhadoifhaodhfaoihdfhasdifhaidhfapfdhiashf"),(UIImage(named: "img_about us_app")!, "Sample Text 1"),(UIImage(named: "banner")!, "Sample Text 2adfahdfkajdfiahdofhadoifhaodhfaoihdfhasdifhaidhfapfdhiashf"),(UIImage(named: "img_about us_app")!, "Sample Text 1"),(UIImage(named: "banner")!, "Sample Text 2adfahdfkajdfiahdofhadoifhaodhfaoihdfhasdifhaidhfapfdhiashf"),(UIImage(named: "img_about us_app")!, "Sample Text 1"),(UIImage(named: "img_about us_app")!, "Sample Text 1"),// Add more sample data here]override func viewDidLoad() {super.viewDidLoad()//        let layout = UICollectionViewFlowLayout() // Create a layout instance
//        collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) // Initialize UICollectionView with the layoutcollectionView.delegate = selfcollectionView.dataSource = selfcollectionView.register(WaterfallCell.self, forCellWithReuseIdentifier: cellReuseIdentifier)collectionView.backgroundColor = .white}// MARK: UICollectionViewDataSourceoverride func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {return sampleData.count}override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellReuseIdentifier, for: indexPath) as! WaterfallCelllet data = sampleData[indexPath.item]cell.configure(with: data)return cell}// MARK: UICollectionViewDelegateFlowLayoutfunc collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {let paddingSpace = sectionInsets.left * (itemsPerRow + 1)let availableWidth = collectionView.frame.width - paddingSpacelet widthPerItem = availableWidth / itemsPerRowlet data = sampleData[indexPath.item]let imageAspectRatio = data.image.size.width / data.image.size.heightlet textHeight = data.text.height(withConstrainedWidth: widthPerItem - 16, font: UIFont.systemFont(ofSize: 14))let imageHeight = min(200, widthPerItem / imageAspectRatio) // Limit image heightlet totalHeight = imageHeight + textHeight + 16return CGSize(width: widthPerItem, height: totalHeight)}func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {return sectionInsets}func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {return sectionInsets.left}func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {return sectionInsets.left}
}
class WaterfallCell: UICollectionViewCell {let imageView = UIImageView()let label = UILabel()override init(frame: CGRect) {super.init(frame: frame)contentView.backgroundColor = .yellowcontentView.addSubview(imageView)imageView.contentMode = .scaleAspectFillimageView.clipsToBounds = truecontentView.addSubview(label)label.numberOfLines = 2label.font = UIFont.systemFont(ofSize: 14)}required init?(coder: NSCoder) {fatalError("init(coder:) has not been implemented")}func configure(with data: (image: UIImage, text: String)) {imageView.image = data.imagelabel.text = data.textlet imageAspectRatio = data.image.size.width / data.image.size.heightlet imageHeight = frame.width / imageAspectRatioimageView.frame = CGRect(x: 0, y: 0, width: frame.width, height: imageHeight)label.frame = CGRect(x: 0, y: imageHeight + 8, width: frame.width, height: labelHeight)}private var labelHeight: CGFloat {let labelWidth = frame.width - 16return label.text?.height(withConstrainedWidth: labelWidth, font: UIFont.systemFont(ofSize: 14)) ?? 0}
}
extension String {func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)return ceil(boundingBox.height)}
}//使用
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
let vc = ConcernedVC(collectionViewLayout: layout)

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/41862.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/41862.shtml
英文地址,請注明出處:http://en.pswp.cn/news/41862.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

產業園區數字孿生3d可視化全景展示方案

隨著數字經濟的發展,數字技術給企業發展帶來了機遇的同時,也為企業管理帶來挑戰。比如園區運維,不僅體量大,復雜的運維管理系統,落地難度也較高。那么如何通過數字化手段重塑園區運營,打通園區各業務數據孤…

SQLyog中導入CSV文件入庫到MySQL中

1.在數據庫中新建一個表,設置列名(與待導入文件一致),字段可以多出幾個都可以 2.右鍵表名,導入- - >導入使用本地加載的CSV數據 選擇使用加載本地CVS數據 3.指定好轉義字符,將終止設置為,號(英文狀態下…

idea常見錯誤大全之:解決全局搜索失效+搜索條件失效(條件為空)+F8失靈

問題一:全局搜索快捷鍵ctrlshiftf 突然失靈了,鍵盤敲爛了 都沒反應,這是為什么呢? 肯定不是idea本身的原因,那么就是其它外在因素影響到了idea的快捷鍵,那么其它的快捷鍵為什么沒失效呢,原因只有…

服務器擴展未生效

服務器擴容未生效 在阿里云付費擴容后,在服務器里面看未生效。 阿里云->實例與鏡像->實例->選擇實例->云盤->擴容進入linux服務器查看: df -h vda1擴容未生效。原40g->擴容后100g 解決方法: 1、安裝growpart yum inst…

Win11環境下 Unity個人版無法激活

網上教程大多都是在win10環境下運行,win11環境下遇到很多沒有碰到的問題,故簡單做個記錄,也方便同樣使用win11的朋友解決問題。 Unity2021無法打開 問題描述:下載Unity2021.3.4f1c1版本(LTS)后&#xff0…

廣州華銳互動:奶牛難產原因及救治VR仿真實訓系統

奶牛難產是一種常見的疾病,對奶牛的健康和生產造成很大的影響。為了解決這一問題,許多奶牛養殖場開始采用VR仿真技術來培訓奶牛獸醫,幫助學生更好地理解奶牛養殖的實際過程,提高他們的實踐能力的教學方式。 VR技術開發公司廣州華銳…

【算法】雙指針劃分思想妙解移動零

Problem: 283. 移動零 文章目錄 思路算法圖解分析復雜度Code 思路 首先我們來講一下本題的思路 本題主要可以歸到【數組劃分/數組分塊】這一類的題型。我們將一個數組中的所有元素劃分為兩段區間,左側是非零元素,右側是零元素 那解決這一類的題我們首先想…

掌握指針進階:一篇帶你玩轉函數指針、函數指針數組及指向函數指針數組的指針!!

🍁博客主頁:江池俊的博客 💫收錄專欄:C語言進階之路 💡代碼倉庫:江池俊的代碼倉庫 🎪我的社區:GeekHub 🎉歡迎大家點贊👍評論📝收藏? 文章目錄 一…

基于Servlet實現的管理系統(包含服務器源碼+數據庫)

資料下載鏈接 介紹 基于Servlet框架的管理系統 簡潔版 ; 實現 登錄 、 注冊 、 增 、 刪 、 改 、 查 ; 可繼續完善增加前端、校驗、其他功能等; 可作為 Servlet項目 開發練習基礎模型; 課程設計 、 畢業設計 開發基礎&…

JVM---jvm里的內存溢出

目錄 堆溢出 虛擬機棧和本地方法棧溢出(棧溢出很少出現) 方法區和運行時常量池溢出 本機內存直接溢出(實際中很少出現、了解即可) 堆溢出 堆溢出:最常見的是大list,list里面有很多元素 堆溢出該怎么解決…

第7章:貝葉斯分類器

貝葉斯決策論 貝葉斯分類器:使用貝葉斯公式 貝葉斯學習:使用分布估計(不同于頻率主義的點估計) 極大似然估計 樸素貝葉斯分類 半樸素貝葉斯 條件獨立性假設,在現實生活中往往很難成立。 半樸素貝葉 斯的一個常用策略…

C++學習筆記4

什么是指針? 指針是存儲內存地址的變量。就像int變量用于存儲整數值一樣,指針變量用于存儲內存地址。指針是一種指向內存單元的特殊變量。 內存單元地址通常使用的是16進制表示(0~9和A~F)來表示數字。顯示…

React源碼解析18(6)------ 實現useState

摘要 在上一篇文章中,我們已經實現了函數組件。同時可以正常通過render進行渲染。 而通過之前的文章,beginWork和completeWork也已經有了基本的架子。現在我們可以去實現useState了。 實現之前,我們要先修改一下我們的index.js文件&#x…

DAY2,ARM(特殊功能寄存器,數據操作指令,跳轉指令)

1.cmp、sub、b指令的使用; 代碼: .text .global _start _start:mov r0,#9mov r1,#15loop:cmp r0,r1beq stopsubcc r1,r1,r0subhi r0,r0,r1b loopstop:b stop .end結果: 2.匯編指令計算1~100之間和; 代碼: .text .gl…

【從零學習python 】47. 面向對象編程中的繼承概念及基本使用

文章目錄 繼承的基本使用代碼逐行講解說明:進階案例 繼承的基本使用 在現實生活中,繼承一般指的是子女繼承父輩的財產,父輩有的財產,子女能夠直接使用。 程序里的繼承 繼承是面向對象軟件設計中的一個概念,與多態、封裝共為面向對…

Android 13 Launcher——屏蔽上拉到應用列表

背景 Launcher定制需要將原先的應用列表去掉,可以從根源去掉,就是將上拉出現應用列表的上拉手勢直接屏蔽,讓其不能上拉出現應用列表界面,在研究的過程中順便將下拉出現負一屏的邏輯也研究了下,如下就是具體實現。 目錄 背景 一.如何屏蔽上拉出現應用列表 一.如何屏蔽上拉…

培訓報名小程序-用戶注冊

目錄 1 創建數據源2 注冊用戶3 判斷用戶是否注冊4 完整代碼總結 我們的培訓報名小程序,用戶每次打開時都需要填寫個人信息才可以報名,如果用戶多次報名課程,每次都需要填寫個人信息,比較麻煩。 本篇我們就優化一下功能&#xff0c…

線上售樓vr全景看房成為企業數字化營銷工具

在房地產業中,VR全景拍攝為買家提供了虛擬看房的全新體驗。買家可以通過相關設備,遠程參觀各個樓盤的樣板間和實景,感受房屋的空間布局和環境氛圍,極大地提高了購房決策的準確性。對于房地產開發商和中介機構來說,VR全…

@Async用哪個線程池

一共可以分三種情況 第一種 未在手動在項目中配置任何線程池 spring boot 會默認添加一個coreSize8的 無界線程池,名稱為applicationTaskExecutor (源碼:org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration&…

如何搭建個人郵件服務hmailserver并實現遠程發送郵件

文章目錄 1. 安裝hMailServer2. 設置hMailServer3. 客戶端安裝添加賬號4. 測試發送郵件5. 安裝cpolar6. 創建公網地址7. 測試遠程發送郵件8. 固定連接公網地址9. 測試固定遠程地址發送郵件 hMailServer 是一個郵件服務器,通過它我們可以搭建自己的郵件服務,通過cpolar內網映射工…