如何通过indexPath添加两个集合视图单元格?

时间:2016-01-21 08:59:20

标签: swift uicollectionviewcell

假设我们有4个这样的集合视图单元格:

What I have

如果我打算在单元格3和单元格4位置替换两个集合视图单元格,如下所示:

What I want

如您所见,单元格3和单元格4关闭,新单元格1和单元格2替换它们的位置。

我有什么方法可以在UICollectionView上做到这一点吗?

我是初学者使用UICollectionView,因为我的所有应用程序开发都充满了tableview用法。所以,我想了解更多。

任何代码帮助都表示赞赏.GitHub回复示例将更受欢迎。

感谢。

1 个答案:

答案 0 :(得分:2)

HERE我拿了一个示例项目进行集合视图,并根据您的要求对其进行了一些修改。

以下是用户在视图上按下按钮时更新集合数组的完整代码。

import UIKit

class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, CustomCollectionViewCellDelegate {

    @IBOutlet weak var overlayView: UIView!

    @IBOutlet weak var collectionView: UICollectionView!

    @IBOutlet weak var overlayLabel: UILabel!

    var purchaseCount:Float = 0.0

    //Your collection view data source at start.
    var cellArray = ["Cell 1", "Cell 2", "Cell 3", "Cell 4"]

    override func viewDidLoad() {

        super.viewDidLoad()
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 50, left: 25, bottom: 50, right: 25)
        layout.itemSize = CGSize(width: 100, height: 100)
        collectionView!.collectionViewLayout = layout
        collectionView!.dataSource = self
        collectionView!.delegate = self
        collectionView!.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
        collectionView!.backgroundColor = UIColor.blackColor()
    }

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {

        return 1
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return cellArray.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! CollectionViewCell
        cell.backgroundColor = UIColor.blackColor()
        cell.textLabel?.text = cellArray[indexPath.row]
        cell.imageView?.image = UIImage(named: "circle")
        cell.imageViewStar?.image = UIImage(named: "star")
        cell.delegate = self
        return cell
    }

    //action when user press button into view
    @IBAction func addNewCell(sender: AnyObject) {

        //Insert an element into your collection array.
        cellArray.insert("New Cell 1", atIndex: 2)
        cellArray.insert("New Cell 2", atIndex: 3)

        //reload your collection view.
        self.collectionView.reloadData()
    }

    // MARK: CustomCollectionViewCellDelegate

    func starImageHit(price:Float) {
        print("star image hit in view controller")
        self.purchaseCount = self.purchaseCount + price
        self.overlayView.hidden = false
        self.overlayView.alpha = 1.0
        self.overlayLabel.text = "\(self.purchaseCount)"
       UIView.animateWithDuration(1.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
            self.overlayView.alpha = 0.0
            self.view.layoutIfNeeded()
        }, completion: nil)
    }

}

你会得到结果:

enter image description here

HERE是您的示例项目。