2016-01-21 62 views
1

比方說我們得到了4個集鑑於細胞是這樣的:如何通過indexPath添加兩個集合視圖單元格?

What I have

如果我要在小區3和小區4的位置,以取代兩個收集觀察室是這樣的:

What I want

正如你所看到的,單元格3和單元格4下降,新單元格1和單元格2取代它們的位置。

有沒有什麼辦法可以在UICollectionView中做到這一點?

我是初學者使用UICollectionView,因爲我所有的應用程序開發充滿了tableview usage.So,我想了解更多。

任何代碼的幫助表示讚賞.GitHub例子的回購將更受讚賞。

謝謝。

+2

也許你可以更新你的數組,然後更新集合視圖。 –

+1

更新你的UICollectionView的dataSource。 – Larme

+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是示例項目適合你。

+0

謝謝,我現在得到了答案,我正在處理與didSelectItemAtIndexPath有關的導航問題。如果我需要幫助,我會回覆你。感謝給我一些像我這樣的初學者。真的很感激。 –

+0

高興地幫助你.. :) –

相關問題