简体   繁体   中英

How to get back the label again while removing from superview in table view cell in swift 3?

I had used the code remove from superView to remove labels from screen when there is no address available just showing one label as shown in below

在此处输入图片说明

but here when I got address from the api and was not displaying properly and the code itself was entering into it is showing in image as shown below

在此处输入图片说明

but the proper image should be displayed with name label , address label and mobile number label can anyone help me how to display the address label and mobile number label after removing from view when address has got from api ?

here is my code

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if (indexPath.section == 0) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! AddressTableViewCell
            let dict = guestShippingAddressModel
            self.tableDetails.isHidden = false
            self.activityIndicator.stopAnimating()
            self.activityIndicator.hidesWhenStopped = true
            cell.deleteButton.tag = indexPath.row
            if self.street?.isEmpty == true || self.street?.isEmpty == nil {
                cell.addressLabel.isHidden = true
                cell.mobileNumberLabel.isHidden = true
                cell.radioButton.isHidden = true
                cell.editButton.isHidden = true
                cell.deleteButton.isHidden = true
                cell.addresslabel.removeFromSuperview()
                cell.mobileNumberlabel.removeFromSuperview()
                cell.nameLabel.text = "No address available"
                if delayCheck == true {
                    let when = DispatchTime.now() + 5 // change 2 to desired number of seconds
                    DispatchQueue.main.asyncAfter(deadline: when) {
                        let storyboard = UIStoryboard(name: "Main", bundle: nil)
                        let addtoCartVC = storyboard.instantiateViewController(withIdentifier: "newAddress") as! NewAddressViewController
                        self.navigationController?.pushViewController(addtoCartVC, animated: true)
                    }
                }
            }
            else {
                cell.addressLabel.isHidden = false
                cell.radioButton.isHidden = false
                cell.editButton.isHidden = false
                cell.deleteButton.isHidden = false
                cell.nameLabel.isHidden = false
                cell.nameLabel.text = "\((dict?.firstName)!) \((dict?.lastName)!)"
                cell.addressLabel.text = "\((self.street)!) \((dict?.city)!) \((dict?.region)!) \((dict?.postCode)!)"
                cell.mobileNumberLabel.text = "\((dict?.telephone)!)"
            }
            cell.radioButton.tag = indexPath.row
            cell.editButton.tag = indexPath.row
            cell.deleteButton.tag = indexPath.row
            cell.editButton.isHidden = true
            cell.deleteButton.isHidden = true
            cell.radioButton.addTarget(self, action: #selector(selectRadioButton(_:)), for: .touchUpInside)
            cell.deleteButton.addTarget(self, action: #selector(deleteAction(button:)), for: .touchUpInside)
            cell.editButton.addTarget(self, action: #selector(editButtonAction(_:)), for: .touchUpInside)
            let checkIndex = self.checkIsRadioSelect.index(of: indexPath.row)
            if(checkIndex != nil) {
                cell.radioButton.isSelected = true
                cell.editButton.isHidden = false
                cell.deleteButton.isHidden = false
            }
            else
            {
                cell.radioButton.isSelected = false
                cell.editButton.isHidden = true
                cell.deleteButton.isHidden = true
            }
            if (checkIsPaymentRadioSelect == true) {
                let defaultvalue = street
                if defaultvalue?.isEmpty == false {
                    cell.radioButton.isSelected = true
                    cell.editButton.isHidden = false
                    cell.deleteButton.isHidden = false
                    addressSelected = true
                }
            }
            return cell
        }

instead of remove address label and mobile label from superview set the height constraint for that labels and change the height constant to 0 to hide the labels and make the cell height dynamic like below.

tblList.rowHeight = UITableViewAutomaticDimension;
tblList.estimatedRowHeight = CGFloat(100)

Man, don't write so many code in dataSource method of UITableViewDataSource . It's very hard to handle and understand what is really going on! There is sooo many strange logic I don't really understand. So I recommend you

  1. to make all configuration in UITableViewCell subclass ( AddressTableViewCell & NewAddressViewController in your case)
  2. Models is better to write like structs / classes but not dictionary
  3. activityIndicator shouldn't be handled in cellForRow method. It should change depending on request whether it's still processing / ended / failed and so on.
  4. If you are doing something like cell.radioButton.tag = indexPath you are doing something wrong cause you have method for tableView .indexPath(forCell: UICollectionViewCell) . Which can say you exact indexPath
  5. If you handle some touches from cell - you should make delegate of the cell which will send to your ViewController the cell where touch occurred like

     func selectRadioButton(inCell cell: UICollectionViewCell) { guard let indexPath = tableView.indexPath(forCell: cell) else { return } // do what you need to do & you have your indexPath! = know where touch occurred } 

Ending my lecture - your source should fully define your cell. It means if you have some flag in the model which indicate on whether you display something or not you should write something like:

 yourView.isHidden = !yourModel.isDisplayed

Why? Because cell are reused and all the subclass properties retain their state that is all UI "settings" (visibility and others remain). Read about it in google there are a lot of articles on this theme which did it better then I am

Hope I helped you!

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