swift 如何在 Collectionview 中显示两个部分和行
How to show two sections and rows in Collectionview in swift
我添加了一个带有约束的集合视图
leading = 10, trailing = 10 top = 10 height = 200
我有两个数组,分别称为 oldArray 和 newArray..我的要求是..如果我最初来自 isEdit,我需要在第 0 部分显示唯一的 oldArray,如果添加新的images(newArray) 然后我需要在第 1 节中显示它们。但是我无法在第 1 节中添加 newArray 图像。我的下面的代码新添加的图像也在第 0 节中添加..
两个数组和集合视图的总代码:
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..这里新添加的图片也在第0节添加,为什么?
如何在第1部分添加新添加的图片,请帮忙
缺少指定节数的方法(默认为 1)
func numberOfSections(in collectionView: UICollectionView) -> Int {
return newArray.isEmpty ? 1 : 2
}
我添加了一个带有约束的集合视图
leading = 10, trailing = 10 top = 10 height = 200
我有两个数组,分别称为 oldArray 和 newArray..我的要求是..如果我最初来自 isEdit,我需要在第 0 部分显示唯一的 oldArray,如果添加新的images(newArray) 然后我需要在第 1 节中显示它们。但是我无法在第 1 节中添加 newArray 图像。我的下面的代码新添加的图像也在第 0 节中添加..
两个数组和集合视图的总代码:
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..这里新添加的图片也在第0节添加,为什么?
如何在第1部分添加新添加的图片,请帮忙
缺少指定节数的方法(默认为 1)
func numberOfSections(in collectionView: UICollectionView) -> Int {
return newArray.isEmpty ? 1 : 2
}