I have added one collection view with constraints
leading = 10, trailing = 10 top = 10 height = 200
I have two arrays called oldArray and newArray.. my requirement is.. if I come from isEdit initially I need to show the only oldArray in section 0 and if add new images(newArray) then I need to show them in section 1.. but I am not able to add newArray images in section 1.. with my below code newly added images also adding in section 0..
total code for two arrays and collectionview:
struct ImagesModel{
public var image : String?
init(image: String?) {
self.image = image
}
}
import UIKit
class BidPlaceVC: UIViewController, UITextViewDelegate {
var oldArray = [ImagesModel]()
var newArray = [UIImage]()
@IBOutlet weak var filesCollectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let width = UIScreen.main.bounds.width/4.1
let height = width*1.1
layout.itemSize = CGSize(width: width, height: 100)
self.filesCollectionView.collectionViewLayout = layout
filesCollectionView.reloadData()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if isEdit{
if section == 0{
return oldArray.count
}
else{
return self.newArray.count
}
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.section == 0{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FilesCollectionCell", for: indexPath) as? FilesCollectionCell
let img = self.oldArray[indexPath.item].image
cell?.imgView.getImage(withUrl: img, placeHolder: #imageLiteral(resourceName: "home"), imgContentMode: .scaleAspectFill)
return cell!
}
else{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FilesCollectionCell", for: indexPath) as? FilesCollectionCell
cell?.imgView.image = self.newArray[indexPath.item]
return cell!
}
}
func uploadServiceCall() {
var imgData = [Data]()
for image in self.newArray {
imgData.append(image.jpegData(compressionQuality: 0.5)!)
}
APIReqeustManager.sharedInstance.uploadMultipleImagesWithParam(url: CommonUrl.place_bid, imagesData: imgData, imageKey: "files", parameters: param, completionHandler: {(responseData) in
if let result = responseData.dict?["result"] as? NSDictionary{
if message == "Success"{
var filesArray = result["files_uploaded"] as? String
self.oldArray.append(ImagesModel(image: filesArray, id: 0))
DispatchQueue.main.async {
self.filesCollectionView.reloadData()
}
}
@IBAction func submitBitBtn(_ sender: Any) {
uploadServiceCall()
}
o/p.. here newly added images also adding in section 0, why?
how to add newly added images in section 1, please do help
The method to specify the number of sections is missing (the default is 1)
func numberOfSections(in collectionView: UICollectionView) -> Int {
return newArray.isEmpty ? 1 : 2
}