Sathish kumar T.M
Sathish kumar T.M

Reputation: 227

How to add cell to dynamic collection view using swift

enter image description here

In top of that image you can see some images and plus button I want to do like this in my app so i used collection view to get this view and everything works fine but i cant add cell to my dynamic collection view can some one help me to do this process

code in my view controller:

 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
    }

}

I have not done any process for adding cell to my collection view i just loaded the data from the server.now help to add the cell

Upvotes: 1

Views: 7163

Answers (2)

dahiya_boy
dahiya_boy

Reputation: 9503

 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        
}

Upvotes: 3

harshal jadhav
harshal jadhav

Reputation: 5684

On click of last cell of the collection view add logic to enter your product in the database then just reload the collection view

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

Upvotes: 1

Related Questions