如何将按钮添加到CollectionView的末尾(不在最后一个单元格中)?

时间:2018-08-20 06:24:55

标签: swift uicollectionview swift4

我如何像在App Workflow中一样将按钮添加到CollectionView的末尾(<设置>末尾)?

我没有找到方法。

工作流程 enter image description here

My app (So big screenshoot)

2 个答案:

答案 0 :(得分:0)

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

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

override func viewDidLoad() {
    super.viewDidLoad()

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

}

第二,在headerReferenceSize中设置collectionViewFlowLayout或在collectionView:layout:referenceSizeForHeaderInSection:中实现UICollectionViewDelegate

第三,从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
}

答案 1 :(得分:0)

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);
    }
}