
为集合视图使用自定义布局.这是执行此 *** 作的正确方法,它使您可以控制单元格如何填充集合视图.
这是来自“raywenderlich”的UICollectionView Custom Layout Tutorial
选项2
这更像是一种做你想做的事情的Hackish方式.在此方法中,您可以按顺序访问数据源以模拟所需的样式.我将在代码中解释它:
var myArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]let rows = 3let columnsInFirstPage = 5// calculate number of columns needed to display all itemsvar columns: Int { return myArray.count<=columnsInFirstPage ? myArray.count : myArray.count > rows*columnsInFirstPage ? (myArray.count-1)/rows + 1 : columnsInFirstPage }overrIDe func collectionVIEw(collectionVIEw: UICollectionVIEw,numberOfItemsInSection section: Int) -> Int { return columns*rows}overrIDe func collectionVIEw(collectionVIEw: UICollectionVIEw,cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionVIEwCell { let cell = collectionVIEw.dequeueReusableCellWithReuseIDentifIEr("Cell",forIndexPath: indexPath) //These three lines will convert the index to a new index that will simulate the collection vIEw as if it was being filled horizontally let i = indexPath.item / rows let j = indexPath.item % rows let item = j*columns+i guard item < myArray.count else { //If item is not in myArray range then return an empty hIDden cell in order to continue the layout cell.hIDden = true return cell } cell.hIDden = false //Rest of your cell setup,Now to access your data You need to use the new "item" instead of "indexPath.item" //like: cell.myLabel.text = "\(myArray[item])" return cell} 以下是此代码的实际 *** 作:
*“添加”按钮只是为myArray添加了另一个数字并重新加载了集合视图,以演示myArray中不同数量项目的外观
编辑 – 将项目分组到页面中:
var myArray = [1,18]let rows = 3let columnsInPage = 5var itemsInPage: Int { return columnsInPage*rows }var columns: Int { return myArray.count%itemsInPage <= columnsInPage ? ((myArray.count/itemsInPage)*columnsInPage) + (myArray.count%itemsInPage) : ((myArray.count/itemsInPage)+1)*columnsInPage }overrIDe func collectionVIEw(collectionVIEw: UICollectionVIEw,forIndexPath: indexPath) let t = indexPath.item / itemsInPage let i = indexPath.item / rows - t*columnsInPage let j = indexPath.item % rows let item = (j*columnsInPage+i) + t*itemsInPage guard item < myArray.count else { cell.hIDden = true return cell } cell.hIDden = false return cell}@H_419_2@ @H_419_2@ 总结 以上是内存溢出为你收集整理的ios – 在Swift中创建水平滚动集合视图全部内容,希望文章能够帮你解决ios – 在Swift中创建水平滚动集合视图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)