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

时间:2017-04-20 05:38:10

标签: ios insert swift2 uicollectionview cell

enter image description here

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

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

 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帮助添加单元格

2 个答案:

答案 0 :(得分:2)

 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        
}

答案 1 :(得分:1)

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

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