[英]How do I make my UICollectionView return 3 columns with 1 pixel spacing between them just like instagram? Swift
I have an extension on a UIViewController
using the UICollectionViewDelegate
and UICollectionViewDataSource
and don't know how to get my cells to layout just like Instagram's. 我在
UIViewController
使用UICollectionViewDelegate
和UICollectionViewDataSource
进行了UICollectionViewDelegate
,并且不知道如何像Instagram一样布局单元格。
extension ProfileViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return userMedias.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? MediaCollectionViewCell else { return MediaCollectionViewCell() }
cell.userMedia = userMedias[indexPath.row]
return cell
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toMediaDetail" {
guard let mediaDetailViewController = segue.destination as? MediaDetailViewController,
let indexPath = collectionView.indexPathsForSelectedItems?.first else { return }
let userMedia = self.userMedias[indexPath.row]
mediaDetailViewController.userMedia = userMedia
}
}
}
Not sure you use storyboard or not. 不确定是否使用情节提要。 If you are using storyboard, cell size can be configure in storyboard.
如果使用情节提要,可以在情节提要中配置单元格大小。 By setting up insetForSectionAtIndex with UIEdgeInsetsMake(0, 0, 1, 1), it should be able to give you 1 px space in between cells.
通过使用UIEdgeInsetsMake(0,0,1,1)设置insetForSectionAtIndex,它应该能够在单元格之间给您1 px的空间。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0, 0, 1, 1)
}
UICollection
Cell with 1px spacing in between: UICollection
单元格之间有1px的间距:
Hope this would help. 希望这会有所帮助。
I am not guarenteeing that these will work, but playing around with these functions is what you are needing to get your results. 我不保证这些功能会起作用,但是要获得结果,需要使用这些功能。 You may only need one of the first two functions, and the third may be optional.
您可能只需要前两个功能之一,而第三个功能是可选的。 There are a lot of different types of results you can obtain here.
您可以在这里获得许多不同类型的结果。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionView, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 1
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(1, 1, 1, 1)
}
// This function may or may not be required depending
// on what data you are using to populate the collection view
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
let collectionViewRect: CGRect = collectionView.bounds
let collectionViewWidth: CGFloat = collectionViewRect.size.width
// Replace userMedias.count with the number of cells in one row
// if that changes or if a row is not constrained to a single line
let cellWidth: CGFloat = collectionViewWidth / userMedias.count
// Replace the divisor with the column count requirement. Make sure to have it in float.
let size: CGSize = CGSize(width: cellWidth, height: cellWidth)
return size
}
You will need to ensure that SizeForItemAtIndexPath provides the appropriate size for a cell that has such insets and then give the insets accordingly. 您将需要确保SizeForItemAtIndexPath为具有此类插图的单元格提供适当的大小,然后相应地提供这些插图。
One last tip... When I am working with collection view I avoid constraining any view to all four walls when using SizeForItemAtIndexPath. 最后一个提示...使用集合视图时,避免使用SizeForItemAtIndexPath将视图限制在所有四个墙壁上。 I try to make proportional width and height while constraining only to the top and left walls of the cell.
我尝试使宽度和高度成比例,同时仅限制到单元的顶部和左侧。 When I don't do this, I have had issues with the cells resizing inappropriately.
当我不这样做时,我会遇到单元大小调整不当的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.