简体   繁体   中英

how to make the view height 0 based on the content of view in swift4

I am new to iOS.I will be having a collection of products.each product will be having min 0 to max 15 details of that product.whenever we click on one product,then relavant details of that product should display in someother view controller.If that product have 5 details ,only 5 details should display.Rest of details fields height should become 0.This is my requirement.I tried this ,but i am gelling an error like "Thread 1: signal SIGABRT".In console "Could not cast value of type 'NSNull' (0x107e57de0) to 'NSString' (0x106ef25d8). 2019-02-15 11:28:51.808090+0530 PlanetZoomApp[6777:85237] Could not cast value of type 'NSNull' (0x107e57de0) to 'NSString' (0x106ef25d8)." If anyone helps me ,would be very great.Thank in advance.

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let lmp = storyboard.instantiateViewController(withIdentifier: "LocatemypicViewController") as!  


 if let desc:String = (dictobj["description"] as! String){

            print(desc)

            lmp.des = desc
        }else{
            lmp.descriptionviewheight.constant=0
        }

self.navigationController?.pushViewController(lmp, animated: true)

    }

When you are using if let, you usually should not use force casting as! , the second problem is constraint is not initialized until viewDidLoad is called. so you can save the value somewhere and use it on internal LocatemypicViewController viewDidLoad.

Overall, I suggest you pass dictobj["description"] to the next view controller and do the logic inside viewDidLoad and avoid doing another view's logic on the parent view controller.

class ViewController1:UIViewController {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let lmp = storyboard.instantiateViewController(withIdentifier: "LocatemypicViewController") as!  


        if let desc:String = (dictobj["description"] as? String){

            print(desc)

            lmp.des = desc
        }else{
            lmp._descriptionviewheight = 0
        }

        self.navigationController?.pushViewController(lmp, animated: true)

    }
}

class LocatemypicViewController:UIViewController {
   var _descriptionviewheight = 0

   override func viewDidLoad() {
      descriptionviewheight.constant = _descriptionviewheight
   }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM