Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

My collection view cell won't display both a label and an image view

My code below is working. However, it is only displaying either the label or image view. I need it to display both. But right now it's only displaying just one of those. I don't know how to fix this. All of the code in my xcode project is below. Please let me know if you have any questions. I have added my collectionview cell

class ViewController: UIViewController {
    @IBOutlet var collectionView : UICollectionView!

    var imagePicker = UIImagePickerController()
    var imgArr = ["bad","bruno","bad","bad","bad"]

    override func viewDidLoad() {
        super.viewDidLoad()
        imagePicker.delegate = self
        imagePicker.sourceType = .photoLibrary
    }

    @IBAction func oneClickAddClll(){
        imagePicker.allowsEditing = true
        present(imagePicker, animated:  true, completion: nil)
    }
}

     extension ViewController : UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imgArr.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        var cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? DataCollectionViewCell
        cell?.imgz.image = UIImage(named: imgArr[indexPath.row])
        cell?.lz.text = imgArr[indexPath.row]

        cell?.index = indexPath
        cell?.deleagete = self
        return cell!
    }
}


    extension ViewController : UIImagePickerControllerDelegate, UINavigationControllerDelegate {

  }

     extension ViewController : DataCollectionProtocoe {
func passData(indx: Int) {
    let vc = storyboard?.instantiateViewController(withIdentifier: "DetailVDC") as? DetailVDC
    vc?.name = imgArr[indx]
    self.navigationController?.pushViewController(vc!, animated: true)
}

func deleteData(indx: Int) {
    imgArr.remove(at: indx)
    collectionView.reloadData()
}


 }



  protocol DataCollectionProtocoe {
func passData(indx:Int)
func deleteData(indx:Int)
  }
  class DataCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var lz: UILabel!
@IBOutlet weak var imgz: UIImageView!

var deleagete : DataCollectionProtocoe?
var index : IndexPath?

@IBAction func press(){

    deleagete?.passData(indx: (index?.row)!)


}
@IBAction func delete(){
    deleagete?.deleteData(indx: (index?.row)!)
}


 }


 extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)


}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let bounds = collectionView.bounds
    return CGSize(width: bounds.width/2 - 10, height: bounds.height / 4)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 20
}
}


 class DetailVDC: UIViewController {
var name = ""

@IBOutlet var img : UIImageView!
@IBOutlet var lbl : UILabel!

override func viewDidLoad() {
    print(name)
    lbl.text = name
    img.image = UIImage(named: name)
}
   }

Comments