【问题标题】:How to add edgeInsets for each cell in Collection view -Swift如何在 Collection 视图中为每个单元格添加 edgeInsets -Swift
【发布时间】:2017-03-14 12:48:07
【问题描述】:

我正在尝试创建一个自定义单元格,每个单元格都有上、左、下间距。是否有任何集合视图数据源方法,以便在每个索引路径中指定单元格插图。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: block.blockCellIdentifier, for: indexPath) as! BlockBaseClass

        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 10

    return cell
    }

但布局不起作用,在 cellforitemAt 索引路径内。

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewlayout


    【解决方案1】:
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
    {
        return UIEdgeInsetsMake(5.0,5.0,5.0,5.0) // top, left, bottom, right
    }
    

    【讨论】:

    • 如何给每个单元格的插图?喜欢每个索引路径
    • 这将默认应用到所有单元格,添加UICollectionViewDelegateFlowLayout
    • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
    【解决方案2】:

    借助这个委托函数,您可以:

    覆盖 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionat section: Int) -> UIEdgeInsets { 返回 UIEdgeInsetsMake(50, 50, 15, 50) }

    这个 UIEdgeInsetsMake ( CGFloat top,CGFloat left,CGFloat bottom,CGFloat right);

    【讨论】:

    • 第一个给出的是自定义edgeInsect的功能
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多