简体   繁体   English

我如何使我的UICollectionView返回3列,它们之间的间距为1像素,就像instagram一样? 迅速

[英]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使用UICollectionViewDelegateUICollectionViewDataSource进行了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.

相关问题 如何更改UICollectionView的标头和其余单元格之间的间距? - How do I change the spacing between the header of a UICollectionView and the rest of the cells? 如何在UICollectionView中实现页面之间的间距? - How do I achieve spacing between pages in a UICollectionView? Swift:UICollectionView中单元格之间的间距不为零 - Swift: The spacing between my cells in my UICollectionView is not zero 如何使用垂直滚动网格在UICollectionView中的列之间设置间距 - How to set spacing between columns in a UICollectionView with a vertical scrolling grid 如何使水平 UICollectionView 在动态单元格之间具有相同的间距 - How to make a horizontal UICollectionView have the same spacing between dynamic cells 删除 UICollectionView 中的行间距? Swift - Remove spacing between rows in UICollectionView? Swift Swift iOS:如何建立Instagram连结? - Swift iOS: How do I make an Instagram link? 你如何确定 UICollectionView flowLayout 中单元格之间的间距 - How do you determine spacing between cells in UICollectionView flowLayout Swift-如何使用uicollectionview做到这一点? - Swift - How could I use uicollectionview to make this? 如何制作视图滚动? 就像Instagram的个人资料一样 - How to make a view scroll? Just like Instagram's profile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM