繁体   English   中英

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

[英]How do I make my UICollectionView return 3 columns with 1 pixel spacing between them just like instagram? Swift

我在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
        }
    }
}

不确定是否使用情节提要。 如果使用情节提要,可以在情节提要中配置单元格大小。 通过使用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单元格之间有1px的间距:

希望这会有所帮助。

我不保证这些功能会起作用,但是要获得结果,需要使用这些功能。 您可能只需要前两个功能之一,而第三个功能是可选的。 您可以在这里获得许多不同类型的结果。

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
}

您将需要确保SizeForItemAtIndexPath为具有此类插图的单元格提供适当的大小,然后相应地提供这些插图。

最后一个提示...使用集合视图时,避免使用SizeForItemAtIndexPath将视图限制在所有四个墙壁上。 我尝试使宽度和高度成比例,同时仅限制到单元的顶部和左侧。 当我不这样做时,我会遇到单元大小调整不当的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM