UICollectionView 3列网格,1px空间?? (包括图片) [英] UICollectionView 3 column grid, 1px space?? (image included)

查看:121
本文介绍了UICollectionView 3列网格,1px空间?? (包括图片)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个带有FlowLayout的UICollectionView,它在垂直或水平方向的单元格之间不留空间。

I have setup a UICollectionView with a FlowLayout that leaves no space in between cells in either the vertical or horizontal directions.

我有一切正常,但有一个第2列和第3列之间的奇数1px空间,我不明白为什么!?我已经验证了1xx差距在iOS模拟器和真实设备上都显示出来。有没有人经历过这个?

I have everything working, yet there is an odd 1px space between the 2nd and 3rd column and I have no idea why!? I have verified the 1px gap shows up both in iOS simulator and on a real device. Has anybody experienced this?

我的UIViewController是以下代理/数据源:

My UIViewController is a delegate/datasource of the following:

class MyViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout

我已经实现了删除任何间距所需的函数(并使用它们正在运行的print语句进行验证) ,以及彼此相邻排列的单元格的视觉确认:

I have implemented the necessary functions to remove any spacing (and verified with print statements that they are running), as well as visual confirmation of the cells lining up next to each other:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
    return UIEdgeInsetsZero
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
    return 0
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
    return 0
}

最后,我打印出宽度被返回的单元格(每个颜色方块)以验证它是(view.frame.width / 3)(320/3),这是106.666667

Finally, I have printed out the width of the cell being returned (each color square) to verify it was the (view.frame.width / 3) or (320/3), which is 106.666667

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    var totalHeight: CGFloat = (self.view.frame.width / 3)
    var totalWidth: CGFloat = (self.view.frame.width / 3)

    println(totalWidth) // this prints 106.666666667

    return CGSizeMake(totalWidth, totalHeight)
}

推荐答案

在类似情况下,最好避免使用数字的双重类型。
使用:

Better avoid number's double type in similar situations. Use:

return CGSizeMake(ceil(totalWidth), ceil(totalHeight))

这篇关于UICollectionView 3列网格,1px空间?? (包括图片)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆