【发布时间】:2021-04-04 07:08:46
【问题描述】:
我能够解析 JSON 并在 Collectionview 中添加单元格.. 但是如果我从这个 Viewcontroller 移动到 viewcontroller 则 collectionview 不会显示.. 但在 JSON 中添加数据
添加collectionview和JSON解析的代码:
class ImageItemModel{
var title: String?
var profileImage: UIImage?
var pic_id: Double?
init(title: String?, imgTitle: UIImage?, pic_id: Double?) {
self.title = title
self.profileImage = imgTitle
self.pic_id = pic_id
}
}
class EditProfileImageViewController: UIViewController {
@IBOutlet weak var titleTextfield: UITextField!
private var imageProfile : UIImage?
private var imagePicker : EasyImagePicker?
@IBOutlet weak var collectionView: UICollectionView!
var arrImageItems = [ImageItemModel]()
@IBAction func imgtitleSaveBtn(_ sender: Any) {
postServiceCall()
}
fileprivate func postServiceCall(){
if titleTextfield.text?.trim() == ""{
return self.view.makeToast("please add service title")
}
let parameters = ["image_title" : titleTextfield.text?.trim() ?? ""]
APIReqeustManager.sharedInstance.uploadMultipartFormData(param: parameters, url: CommonUrl.edit_profile_images, image: imageProfile, fileName: "image", vc: self, isHeaderNeeded: true) {(responseData) in
print("edit profile result \(responseData)")
if let result = responseData.dict?["result"] as? NSDictionary{
let success = result["status"] as? [String : Any]
let message = success?["message"] as? String
if message == "Success"{
let image = result["image"] as? [String : Any]
let picId = image?["id"]
self.arrImageItems.append(ImageItemModel(title: self.titleTextfield.text, imgTitle: self.imageProfile, pic_id: picId as! Double))
self.collectionView.reloadData()
}
else{
self.view.makeToast(CommonMessages.somethingWentWrong)
}
}
}
}
extension EditProfileImageViewController : UICollectionViewDelegate,UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arrImageItems.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCollectionViewCell", for: indexPath) as! ImageCollectionViewCell
cell.imgView.image = arrImageItems[indexPath.item].profileImage
cell.lblTitle.text = arrImageItems[indexPath.row].title
cell.deleteButton.tag = indexPath.row
cell.deleteButton.addTarget(self, action: #selector(deleteService(sender:)), for: UIControl.Event.touchUpInside)
return cell
}
}
使用上面的代码,我可以添加 collectionview 单元格并能够以 JSON 格式存储数据,但是.. 如果我从这个 viewcontroller 移动并返回到这个 viewcontroller,那么 collectionview 不会显示,为什么?怎么了?请帮我写代码。我卡在这里很久了。
【问题讨论】:
-
尝试在主线程上重新加载集合视图,看看是否能解决问题。
-
@KeyhanKamangar,我尝试在主线程中重新加载.. 但相同.. 这是总代码.. 我用来显示 collectionview.. 但如果我从这个 VC 移动并回来我是.没有得到collectionview ..请帮忙..我被困在这里很久了
标签: json swift uicollectionview