如何使用 swift 将单元格添加到动态集合视图

How to add cell to dynamic collection view using swift

在该图像的顶部你可以看到一些图像和加号按钮我想在我的应用程序中这样做所以我使用集合视图来获取这个视图并且一切正常但我无法将单元格添加到我的动态集合视图有人可以帮我做这个过程吗

我的视图控制器中的代码:

 import UIKit
import Alamofire
import SwiftyJSON
import SDWebImage
import SwiftElegantDropdownMenu
class EditPendingViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UITextViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate{



var pendingImageString:[String]!

 @IBOutlet var editPendingCollectionvIEW: UICollectionView!
 override func viewDidLoad() {
        super.viewDidLoad()

 }

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

        return pendingImageString.count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("editPendingCell", forIndexPath: indexPath) as!MypendindeditCollectionViewCell
        let img:NSString = pendingImageString[indexPath.item]
        cell.editImage.sd_setImageWithURL(NSURL(string: imageApikey + (img as String)))
        cell.contentView.layer.borderColor = UIColor.lightGrayColor().CGColor
        cell.contentView.layer.borderWidth = 1.0
        return cell
    }

}

我没有完成任何将单元格添加到我的集合视图的过程我只是从 server.now 帮助加载数据以添加单元格

 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return pendingImageString.count + 1 // + 1 for last cell
}

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

    let cell : CollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell
     cell.btn.tag = indexPath.row

     if indexPath.item = pendingImageString.count {
       cell.imageview = //your static image that is shown in last cell
       cell.cancelButton.isHidden = true
     }
     else{
         let img:NSString = pendingImageString[indexPath.item]
         cell.editImage.sd_setImageWithURL(NSURL(string: imageApikey + (img as String)))
         cell.cancelButton.isHidden = false
     }

}

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
     if indexPath.item = pendingImageString.count{
       // update your array with new image file
     }
}

// cancel button action
func buttonClicked(sender: UIButton?) {
    let tag = sender.tag
        // remove object from array and reload collectionview        
}

点击集合视图的最后一个单元格添加逻辑以在数据库中输入您的产品然后重新加载集合视图

   func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.row == pendingImageString.count - 1
        {
            //TODO - add item to database and reload collectionview
        }
    }