【问题标题】:How add button to end of CollectionView (Not in last Cell)?如何将按钮添加到 CollectionView 的末尾(不在最后一个单元格中)?
【发布时间】:2018-08-20 06:24:55
【问题描述】:

如何将按钮添加到 CollectionView 的末尾就像在应用程序工作流中一样(设置按钮在末尾)

我没有找到如何做到这一点。

工作流程

My app (So big screenshoot)

【问题讨论】:

标签: swift uicollectionview swift4


【解决方案1】:

这可以使用UICollectionView footerView 来实现。下面是一个如何工作的示例:

首先,在ViewDidLoad注册你的页脚视图类:

override func viewDidLoad() {
    super.viewDidLoad()

    registerClass(myFooterViewClass, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "Footer")

}

其次,要么在collectionViewFlowLayout 中设置headerReferenceSize,要么在UICollectionViewDelegate 中实现collectionView:layout:referenceSizeForHeaderInSection:

第三,从dataSource返回footerView:

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
     let view = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionFooter, withReuseIdentifier: "myFooterView", forIndexPath: indexPath)

     // Add your button here

     return view
}

【讨论】:

    【解决方案2】:
    extension ViewController: UICollectionViewDataSource {
        func numberOfSections(in collectionView: UICollectionView) -> Int {
            return 5
        }
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 1
        }
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            if indexPath.section != 5 {
                //without button Cell
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: contentCellIdentifier, for: indexPath) as! CollectionViewCell
                return cell
            } else {
                //with button Cell like setting Click
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: contentCellIdentifier, for: indexPath) as! CollectionViewCell
                return cell
            }
        }
    }
    
    extension ViewController:UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            if indexPath.section == 5 {
                //open your viewController in which you want to setting view like.
            }
        }
        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
            return CGSize(width: screenWidth/2, height: screenWidth/3);
        }
    }
    

    【讨论】:

    • 虽然此代码 sn-p 可能是解决方案,但包含解释确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    相关资源
    最近更新 更多