iosarraysswifticarousel

How can I remove view from iCarousel and reload data


I am relatively new to IOS Development and I have implemented iCarousel in one of my ViewControllers. I am passing some data through a String array, and when I click a button on the carouselView, it should open up the photo library. Once I select an image from the photo library, I want to remove the value from the array and remove that instance of the iCarousel card.

I have looked around and have only found code in obj-c but I am using Swift.

I attached a picture of the iCarousel. When I click on the camera button, it will take me to the photo library, but once I select the photo, I want that instance of the carousel card removed. ViewControllerImage

here is the code I am using for the iCarousel currently in the ViewController

    class ChallengesViewController: UIViewController, TabItem, iCarouselDelegate, iCarouselDataSource {
    var tabImage: UIImage?
    
   
    var image:UIImage? =  imageLiteral(resourceName: "camera")

    var masterchallengeList = [String]()
    var masterSubtitles = [String]()
    var weeklySelect = [String]()
    var weeklySubtitleSelect = [String]()
    
    enum StorageType {
        case userDefaults
        case fileSystem
    }
    
    @IBOutlet weak var carouselView: iCarousel!
    
    
    override func awakeFromNib() {
        masterchallengeList = [“A “, “B”, “C”, “D”, “E”, F”, “G”]
        
        masterSubtitles = [“A “, “B”, “C”, “D”, “E”, F”, “G”]


        weeklySelect = ["","","","","","",""]
        weeklySubtitleSelect = ["","","","","","",""]

        
        for n in 0...(weeklySelect.count - 1){
            weeklySelect[n] = masterchallengeList[n]
        }
        for n in 0...(weeklySubtitleSelect.count - 1){
            weeklySubtitleSelect[n] = masterSubtitles[n]
         }
    }


    @objc func didTapCameraImage() {
        
        presentPhotoActionSheet()
        
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        
        carouselView.type = .coverFlow
        
        navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
        navigationItem.titleView?.tintColor = UIColor.init(red: 3, green: 25, blue: 82, alpha: 1.0)
        navigationController?.navigationBar.prefersLargeTitles = true
        navigationItem.title = "Challenges"
        
        
   
    }
    

    func numberOfItems(in carousel: iCarousel) -> Int {
        return weeklySelect.count
    }
    
    
    
    func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
         let tempView = UIView(frame: CGRect(x: 0, y: 0, width: 250, height: 350))
        
        
          let lightBlue = UIColor.init(red: 94, green: 122, blue: 255, alpha: 1.0)
        
        
           tempView.clipsToBounds  = true
           tempView.translatesAutoresizingMaskIntoConstraints = true
           tempView.layer.cornerRadius = 25.0

            

        
        let actionLabel = UILabel(frame: CGRect(x: 20, y: 0, width: 200, height: 150))
        actionLabel.numberOfLines = 0
        actionLabel.font = actionLabel.font.withSize(25.0)
        actionLabel.textColor = UIColor.white
        actionLabel.text = weeklySelect[index]
        tempView.addSubview(actionLabel)
        
        let subtitleLabel = UILabel(frame: CGRect(x: 20, y: -30, width: 200, height: 150))
        subtitleLabel.center.y = tempView.center.y
        subtitleLabel.numberOfLines = 0
        subtitleLabel.font = actionLabel.font.withSize(14)
        subtitleLabel.textColor = UIColor.white
        subtitleLabel.text = weeklySubtitleSelect[index]
        tempView.addSubview(subtitleLabel)

        
        let photoButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
        photoButton.center.x = tempView.center.x
        photoButton.center.y = tempView.center.y + 92.5
        photoButton.setImage(image, for: .normal)
        photoButton.setImage( imageLiteral(resourceName: "camera"), for: .normal)
        let gesture = UITapGestureRecognizer(target: self,
                                             action:#selector(didTapProfilePic))
        photoButton.addGestureRecognizer(gesture)
        
        tempView.addSubview(photoButton)
        
        return tempView

    }
    
       func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {
       
       if option == iCarouselOption.spacing{
        return value * 2
       }
       

       
       return value
      }
    
    

       }


    extension ChallengesViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate{
    

   
    func presentPhotoActionSheet() {
        let actionSheet = UIAlertController(title: "Profile Picture", message: "How would you like to select a profile picture", preferredStyle: .actionSheet)
        
        actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        actionSheet.addAction(UIAlertAction(title: "Take Photo", style: .default, handler: { [weak self] _ in
            self?.presentCamera()
        }))
        actionSheet.addAction(UIAlertAction(title: "Choose Photo", style: .default, handler: { [weak self] _ in
            self?.presentPhotoPicker()
        }))
        
        present(actionSheet, animated: true)
    }
    func presentCamera() {
        let vc = UIImagePickerController()
        vc.sourceType = .camera
        vc.delegate = self
        vc.allowsEditing = true
        present(vc, animated: true)
        
    }
    func presentPhotoPicker(){
        
        let vc = UIImagePickerController()
        vc.sourceType = .photoLibrary
        vc.delegate = self
        vc.allowsEditing = true
        present(vc, animated: true)
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        picker.dismiss(animated: true, completion: nil)
        
        guard let selectedImage =  info[UIImagePickerController.InfoKey.editedImage] as? UIImage else{
            return}
        image = selectedImage
      
        }
    

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }
    override func present(_ viewControllerToPresent: UIViewController,
                                   animated flag: Bool,
                                   completion: (() -> Void)? = nil) {
               viewControllerToPresent.modalPresentationStyle = .fullScreen
               super.present(viewControllerToPresent, animated: flag, completion: completion)
             }
    
    @objc func dismissAlert(){
        customAlert.dismissAlert()      }
}

Solution

  • If you want to just hide it iCarousel

    carouselView.isHidden = true
    

    But, if you want to remove it from its superView you must do it

    carouselView.removeFromSuperView()
    

    this line remove the carouselView from it's superView so in this case its self.view

    Maybe you do not want to use carouselView after the removing from superView. In this case you can go with;

    carouselView = nil
    

    so use

    carouselView.removeFromSuperView()
    carouselView = nil
    
    
    
    weeklySelect.remove(at: selectedRowIndex)